Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on socket server.. 2

Status
Not open for further replies.

matthewking

Programmer
Jul 15, 2002
75
ES
Hi,

Im trying to create a socket library which doesn't use TcpClient or TcpListener..

I've written the client, which seems to work fine. On the server side so far I have SocketServer and ClientHandler classes, when the server accepts a connection it passes the socket to a new instance of ClientHandler to manage the io for the socket. But im having various problems, I really need a quality example.

Does anyone know/could post a quick example of a socket server that accepts connections, hands them to a client handler and can send, recieve data? Would be a great help, all I can find is TcpClient, TcpListener examples.

Thanks,

Matt.
 
>> Does anyone know/could post a quick example of a socket server ... [lol]

I don’t know why so many people think a socket server is something quick and simple. Unless you use something like Java.NIO you have to handle threads as well as the socket API’s. Of course when threads are involved, usually synchronization is an issue as well. So looking at a shopping list you have:

Socket create/configure
Socket connect
Socket read/write
Threads
Synchronization

This will take some time. You can’t just copy/paste a Socket Server even in C#. Here is basic hint of starting a Socket Server. This is incomplete code you have to finish it by looking at the documentation to fill out the rest.
Code:
System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(....);
sock.Bind( ...);
sock.Listen(...);
System.Net.Sockets.Socket sessSock = sock.Accept();

-pete
 
Thanks PalBano, I've got the socket stuff sorted, I think its things like how im listening for connections etc.

When I start the server socket, I bind the port to the socket and call its Listen method, then I have a while(true) loop with MySocket.Accept with a callback method, is this correct?

In the callback for AcceptClientConnection I have:

ClientHandler ch = new ClientHandler(
MySocket.EndAccept(ar));

Which I believe passes the socket for this client to a ClientHandler class, which takes it as a argument.

In ClientHandler, I do what? start a endless loop listening for data from the client?

As you can see I've looked at the librarys for this kind of stuff, just aren't sure how to put it all together, I can't find a single example online that doesn't just connect, send data, wait for a response and disconnect.

Any advice, guidance would be very appreciated.

Matthew.
 
This is where what Palbano was talking about multi-threading comes in.

The idea is that after accepting a new socket, you start a new thread and pass the socket to it as a parameter. In that thread, it is able to make blocking calls to receive() (the code waits for the other end to send you something). Once you receive a few bytes, you can then act on them.

Which is where the synchronization that Palbano mentioned comes in. If you need to access anything -- ANYTHING!! -- outside your thread, it must be synchronized so that two threads don't step on each other. The framework has at least two synchronization objects that I can remember (It's midnight, and I don't have my help file up) - the Sync C# method and the Monitor class.

Hope this gets you started. Remember that the sockets library in .NET is similar to the Berkeley Sockets specification, so a lot of the Unix material out there will at least be somewhat helpful.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Ok, Got the server/client working well. Thanks for the help palbano & ChipH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top