开发者

Socket Programming -Java - Many Clients One Socket Question(s)

Essentially Im trying to get many many java clients connect to a socket on my ColdFusion server (Using the Socket Gateway). However before i even start to code this, Im a little confused about sockets and their performance. First of all, are sockets meant for many(1000+) clients connecting to one socket (say port 2202) on one server?开发者_Python百科 How is the performance if all there waiting for is basically a ping, or something such that when these clients receive this "ping" they can go get some new data.

Thanks, Faisal Abid


Socket is identified by following tuple,

  1. Source IP
  2. Source Port
  3. Dest IP
  4. Dest Port
  5. Protocol (TCP or UDP)

Even 1000 clients all connect to the same port (dest port), each will get its own socket. So you will have 1000 sockets open.

It's going to be tough to maintain 1000 sockets with blocking I/O, which usually means 1000 threads. You need to use NIO. We have a server written with Mina, which can handle 2000 connections at peak.


First of all, are sockets meant for many(1000+) clients connecting to one socket (say port 2202) on one server

Yes, your server will open a socket on port 2202, and 1000 client will connect to it. Server open server socket, and client will open client socket, it different.

How is the performance if all there waiting for is basically a ping, or something such that when these clients receive this "ping" they can go get some new data

On server, you use getInputStream function to get data from client, and getOutputStream function to send data to Client.

Notice: you should use Thread to process each request of client


There is nothing wrong with many socket clients that use blocking I/O (you may have a look at this article for more information about that). However, there are another approaches that can better fit your needs here:

  • nio;
  • multicasting;


Hi connecting with 1000 clients at the same time using simple java socket programming is not the perfect way. The probelm is that in fedora linux default maximum no of file can be open is 1024 and in windows it is 2048 or something like that. so in your server side you will find more than 1000 files open and after that if client will go on increaing then you will find too many files opnened error. so the better way is to use non blocking socket programming (I mean to say to use SocketChannel) Using socket channel at the same time as per i have check we can connect with 20,000 clients without any problem.

So better use nio. there is a very good book from Oreilly publication for java nio. I have used java nio (socket channel)

Thanks Sunil Kumar Sahoo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜