Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Server and Client Socket help

DevelopmentDevelopment Member Posts: 222
Hey can anyone help me with this problem I am trying to make a welcome message on my tcp program via server to client here is my server code :

[code]

procedure Tc_form_main.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
FullString := Socket.ReceiveText; // Recieves FullString;
User := Copy(fullstring,1,5); // Gets Frist 5 Letters In FullString
UserText := Copy(fullstring,6,Length(FullString)); // Gets All The Letters After The 5th Character

if User = 'USER!' then
begin

// For loop so you can seed it to all the clients
// listbox index start from 0 thats why I do -1

for i := Listbox1.Count -1 downto 0 do
begin
ServerSocket1.Socket.Connections[i].SendText('USER!');
ServerSocket1.Socket.Connections[i].SendText(UserText);
end;
end;

// Send a welcome msg to all clients ( THIS IS THE PART I AM WORKING ON TRYING TO SEND A WELCOME MSG TO ALL CLIENTS )

if User = 'MSG!' then
begin
ServerSocket1.Socket.Connections[i].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
end;

// User has connected to chat add name to listbox

if User = 'CCON!' then
begin
Listbox1.Items.Add(UserText);
if Listbox1.Count > 1 then
begin
for I := Listbox1.Count -1 downto 0 do
begin
ServerSocket1.Socket.Connections[i].SendText('CCON!');
ServerSocket1.Socket.Connections[i].SendText(Listbox1.Items.Text);
end;
end
else
begin
ServerSocket1.Socket.Connections[0].SendText('CCON!');
ServerSocket1.Socket.Connections[0].SendText(Listbox1.Items.Text);
end;
end;

// User has disconnected from chat remove name from listbox

if User = 'DISC!' then
begin
If ListBox1.Count > 1 then
begin
for i := Listbox1.Count - 1 downto 0 do
begin

// StrPos Checks and see if the strings match

if StrPos(PChar(UserText),PChar(listbox1.Items.Strings[i])) <> nil then
listbox1.Items.Delete(i);
end;
end

// don't need a for loop if there is only one person in the listbox

else
Listbox1.Clear;
for i := Listbox1.Count -1 downto 0 do
begin
ServerSocket1.Socket.Connections[i].SendText('CCON!');
ServerSocket1.Socket.Connections[i].SendText(Listbox1.Items.Text);
end;
end;
end;

[/code]

Client code

[code]

procedure TfrmMain.Sock1Read(Sender: TObject; Socket: TCustomWinSocket);
var
FullString, Msg, After5Char, First5Char : String;
Buffer : Array[0..255] of char;

begin
case ClientStatus of
csIdle:
begin
Socket.ReceiveBuf(Buffer,5);
First5Char := Copy(buffer,1,5);
After5Char := Copy(FullString,6,Length(FullString));

if First5Char = 'CCON!' then
ClientStatus := csOnlineUsers
else
if First5char = 'USER!' then
ClientStatus := csUserText
else
if First5char = 'MSG!' then
ClientStatus := csMsg
end;

csOnlineUsers:
begin
OnlineUserListBox.Clear;
FullString := Socket.ReceiveText;
OnlineUserListBox.Items.Text := fullstring;
ClientStatus := csIdle;
end;

csUserText:
begin
Fullstring := Socket.ReceiveText;
txtMain.Lines.Add(FullString);
ClientStatus := csIdle;
end;

( THIS IS WERE MSG WILL COME AND BE SEND TO TXTMAIN WERE EVERYONE CAN SEE IT )

csMsg:
begin
FullString := Socket.ReceiveText;
txtMain.Lines.Add(FullString)
end;
end;
end;

[/code]
Slewis
Sign In or Register to comment.