problem of socket

I want to build one socket that is looked as server and it can only be connected with special IP address with same port, for example,two client IP and want to connect with my socket with port 8888,and only the connection of is valid,Can I recognize the correct connection?that means my socket accept() does’t respond to the,Is it possible?

I don’t think accept can filter that way. Once you get the connection, you can throw it away based on the IP if you like.

I ask the problem as before we build the programme structure that we start two processes,one is prepare to connect with,the another is prepare to connect with,they use different port and run ok,but now they need use the same port and the two process can distinguish the client that it should be connected.It’s not easy to change programme structure now.

Let me see if I understand this? You want to separate processes on cpu 1, and you want two clients to be able to connect to them via the same port. You want a specific client to connect to a specific process. Is that right?

I don’t think two processes can listen on the same port, but even if they can, there is no mechanism to assure that a client goes
to a specific process based on IP.

It looks like you need to redesign. I can’t see why that would be a problem.
Why not just use two different ports?

No it’s not possible, accept will accept/listen will accept all connections, there is no way to filter them based on IP address, if they are on the same segment. You could bind the socket to an interface but in your case that doesn’t seems applicable. However you should be able to accept it, then detect the IP adress of the source and the close the connection if it’s invalid?

I think the idea “port” is to seperate “programs”. (IP is to point to a machine, PORT is to a program in that machine)
So have 2 process listen on same port and try to make a difference doesn’t sound too good.

If you insist, you can, however:

  1. make your 2 programs as 2 threads in one program, and use another thread to listen()/accept() connection,
    check the src IP, and pass the request to one of the thread in your choice.

or, if you insist 2 processes:

  1. make another process to listen()/accept() connection, and pass the connection on to different process,
    based on the src IP. Somewhat like “inetd listen() on all the ports, and pass the connection onto different