INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: TCP Chat Client \ Server  (Read 2305 times)

KingSparta

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 20063
TCP Chat Client \ Server
« on: June 27, 2003, 03:35:09 pm »

Small Question

Can You Have More Than One Connection On A Winsock Connection? (Using MS Winsock)?

It Seems That All The Samples Of A Chat Program Are Only Client To Client Server.

And Trying To Have 2 Clients On One Server Seems To Reject Client #2

================================================================

I did play with something today that sort of worked.

Server would Listen, User send message, client logs on to server, sends message, and logs out.

========= My theory ===========

if I set the client to Logon to server every 15 seconds and check In it could then get any message in That clients buffer.

After lets say 1 min and client has not checked in remove client from the server list and delete the users message buffer.

Anyone have any ideas?
Logged
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio, Music
https://MyAAGrapevines.com
https://centercitybbs.com
Fayetteville, NC, USA

RemyJ

  • Regular Member
  • Citizen of the Universe
  • *****
  • Posts: 1249
Re: TCP Chat Client \ Server
« Reply #1 on: June 28, 2003, 04:38:20 pm »

There are lots of ways to do this but how you do it depends on things like multi-threading skills and need for reliable delivery, etc.

Here's an option for TCP...

Open a new STREAM socket (the listen or bind socket) bind it to a port then call listen on it.

Call accept in a loop so that each time accept returns (which will be the result of a client connecting) create a new thread and pass the socket returned by accept (the client socket) to it.  You can add the socket to an array to keep track of connected clients.  The new thread then sits in a loop blocked on a recv.   When a client sends data, the recv will unblock and you can do what you want with the data.   You send data to the client by finding the appropriate client's socket in the array and just "send"ing to it from the UI thread.  

When recv returns an error instead of data, it usually means that the client disconnected and you can do whatever cleanup you need including terminating the client thread.

Before threading became popular, the way to handle multiple clients was to use select.   It monitors multiple sockets and tells you what sockets are ready to be read from or have errors.   If you plan on handling lots of clients, this is actually a better way because it prevents having to have an thread for each connected client.

Another option is to use UDP (a DATAGRAM or connectionless socket).  A single socket can receive message from any number of clients and can be easier to program.  The downside is that UDP isn't a guaranteed delivery mechanism.  You have to decide if losing a message is acceptable and if not, you'll have to write your own retransmission mechanism.


Logged
Fedora 40 x86_64 Xfce

KingSparta

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 20063
Re: TCP Chat Client \ Server
« Reply #2 on: June 28, 2003, 05:24:51 pm »

been thnking about most of that today.

I had to buy a book About TCP/IP today did a bit of reading & Gateley ppointed me into a better direction.

I do have a server working now (6 users)

It monitors multiple sockets

the client will start at a port and when it finds one it can connect to it goes in and server accepts connection.

the server Gets, the Username, IPAddress, Email (If Provided by user) etc....

seems to work good.

I might have something, better by tomorrow night

thanks for the info


Logged
Retired Military, Airborne, Air Assault, And Flight Wings.
Model Trains, Internet, Ham Radio, Music
https://MyAAGrapevines.com
https://centercitybbs.com
Fayetteville, NC, USA
Pages: [1]   Go Up