PDA

View Full Version : Server Socket-Client Socket error...have no clue ..help!



Voltrox
15-09-2006, 11:03 PM
When I try to send text from my server to the client but not in an onconnect or onrecieve event, but from a button click even when they are connected, it gives this message and fails to send it.

But when it is in an onconnect or onrecieve event it sends the message and with no trouble.

Could someone please help me?

Socket not connected or address not supplied.

procedure THXDSK.Button1Click(Sender: TObject);
begin
HDSSOCK1.Socket.SendText('Test 2');
end;

Thank you.

TheDon
17-09-2006, 10:28 PM
Hello Voltrox!

A TCP Server can handle multiple connections at the same time. Therefore the serversocket (your HDSSOCk1.Socket) only listen for new incoming connections (at your specified port number). If a new connection is made, a new socket is created by the server's listening socket.

In your app, you try to send data over this listening socket, which is not possible, because the listener do not know, where to send data. If you send some data in the OnConnect or OnReceive event, the correct socket for this connection is passed as parameter.

The available connection-sockets are stored in the list HDSSOCK1.Socket.Connections. There is an entry for each connection.
To send some data to each connection do:



procedure THCSDK.Button1Click(Sender : TObject);
var i : integer;
begin
for i := 0 to HDSSOCK1.Socket.Connections.Count - 1 do
HDSSOCK1.Socket.Connections[i].SendText('Test 2'+#13);
end;


Keep in mind, that you may have some "dead" sockets in the Socket.Connections list, beceause of connection losts. TCP is not trivial for beginners. For simple task I prefere UDP, I only use TCP for bigger data transfers like database or file transfers.

HTH

Voltrox
17-09-2006, 11:43 PM
Thank you. :)

That was very helpful...

But...How do I know which socket each client is connected to? is there an easy way to identify it? :S

I see the UDP socket components in Delphi 2006, but I have no idea how to use them, could you help me? :(

I heard that UDP has error problems and some drawbacks, how can I avoid these?

Thank you very much.

TheDon
18-09-2006, 11:16 AM
You are wellcome!

Each TCP connection is identified by the server IP and Port and client IP and Port (you have this information available at the servers and client side). But I do not think that this information has any purpose for you. Do you need to differentiate between the clients (from the server's view) or are all clients identical?

Do the server need to send information to some clients without a request form the client? If that is true, you should use threaded TCP connections (which can be an advanced topic).

Give me a list of features you server has to serve, and I can give you some advice how to implement it.

Do you mean the Indy components (starts with Tid*)?

UDP is packet oriented (TCP is connection oriented) but a successful data transfer is not guaranteed by the protocol. You won't get any error message if a UDP package is lost or can not be transmitted. With an TCP connection the transfer is guaranteed by the protocol layer and TCP have built in retransmit. There it has a lot more overhead than UDP.

UDP is usefull for short messages, for request/response communications between server and client or for regular status updates from an machine. But UDP can not handle multiple connections (because there is no connection).

It is difficult to explain for me because english is not my mother tongue.

Voltrox
18-09-2006, 06:27 PM
Yes, I need to be able to differentiate between the clients.

Yes, I need it to send information without requests.

No, not the Indy (but I would really like to learn how to use Indy, how would I? I don't see any tutorials), i'm using TServersocket.

It's ok, what is your first language?

TheDon
18-09-2006, 10:52 PM
Demos for Indy 9 or 10 are available for download on:
http://www.indyproject.org/Sockets/Demos/index.de.aspx

HTH

Voltrox
21-09-2006, 12:38 AM
I tried the demo there (only one is there :( ) and it is for SSL only, and it also had problems and wouldn't compile.

Diaboli
21-09-2006, 06:33 AM
i only use TServerSocket and TClientSocket with no problem.

To send to one specific client, you can store its socket.

f.eks:

Client[ThisClient].Socket:= Socket;

That way you can enter this later on:
CLient[AClient].Socket.SendText('hello!');

TheDon
21-09-2006, 01:37 PM
http://www.atozed.com/indy/demos/10/index.de.aspx

The "Zip Code Server (OnExecute)" and the "Intro TCP Client" are good introductions.

You can connect to the server with "telnet localhost 6000". It is a text-based multi-threaded server.

Voltrox
22-09-2006, 11:52 PM
Diaboli, I don't fully understand...Could you explain it in a simpler way?

thanks...