Click here to Skip to main content
13,087,941 members (66,343 online)
Rate this:
Please Sign up or sign in to vote.
Hi, I'm trying to write a client application for a server which I have no documentation of. The Communication is done over TCP/IP.

What I know of the server is:
It's on another machine on a LAN
It listens on port 4001
It Receives messages on port 4001
It sends messages to the client's TCP port 5001

The Client is supposed to send request messages and receive server replies.

Here's how I built the client:

Created one client socket and bound it to port 5001

Connected the socket to the server using the connect(...) function
from winsock2.h, using destination port 4001.

Run a while(1) loop which gets user input and calls the send(...)

Run a thread which calls the recv(...) function, since the client
doesn't know when the server will send a message (sometimes it sends
messages to get the status of the client)

This all runs fine when testing it with a server app I built. The server app does the following:

Creates a Socket and binds it to port 4001
listens for a connection
after getting a connection request, it accepts the client and returns
a pointer to a socket. The server uses this pointer to send and
receive messages to the client.

With this setup, the client can send/recv as many messages, in any order, and it works fine.

The problem is that when testing against the proprietary server, I can send messages from the client, and the server tries to reply, but the replies do not reach the client's port. The server's log says (could not send message to client..., not sure what it means as I do not have the documentation), but using wireshark, I see the TCP data getting to the server, and the servers replies coming sometimes from port 4001 and sometimes from other ports like 3669.

There is a client application already built (it is proprietary so I need to build one myself), and it communicates well with the server.

I honestly don't know that is wrong with my client, as I understood, clients don't need to bind to a port, but I did it because I know for a fact the the server sends messages to that port (5001). Also, I am new with sockets, and what I get is:

client creates a socket (in my case, binds it to port 5001)
client connects to server
client can now send/recv messages

server creates socket
server binds socket to port 4001
server listens
server accepts and returns pointer to client socket
server uses pointer to send/recv messages

Works with test applications, not with server.

can anyone help out?

The site warned me that my message was too long, and I apologize, I just want to give all the details. I will post code upon request.

Thanks in advance
Posted 20-Jul-10 16:01pm
E.F. Nijboer 21-Jul-10 4:25am
On which port the client messages come in after they connected? Could it be that they still use port 4001?
When the server accepts the client and sends the client a new port to use. The client should disconnect and reconnect to the port that it just got. This means that the connection has to be set up all over again for a second time, and this time the server also listens to the socket it gave to the client and after connecting it also uses this for sending/receiving with the client. Don't mix that up!

Good luck!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

From your description it would appear that your client needs two parts.

The sender portion should create an arbitrary socket which connects to the server port 4001 and communicates as necessary.

The client also needs a listener socket bound to port 5001 which will accept connections from the server and communicate through that socket as necessary.
figassis 22-Jul-10 15:10pm
Reason for my vote of 5
Short, correct and to the point
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you accept the general definition that a server is something that listens for a connection you haven't really got a straight client and server here... Both what you're calling your client and what you're calling the server are actually both clients and servers.

So your "server":
- listens on port 4001
- when it accepts a connection it opens a connection to your "client" on port 5001

Your client
- when it makes a connection on the server's port 4001 it listens on port 5001

To be honest I can't see a lot of point to this architecture as generally you get a duplex connection whenever you open a socket. It looks like the original implementer of the system either didn't understand sockets or there was a reason he or she managed their own send/recieve pipes (maybe a bug in the TCP implementation? Maybe one of the channels is UDP and not TCP...)


figassis 22-Jul-10 15:11pm
Reason for my vote of 5
Very clear
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Thank you all so much, and I actually got it working before coming back to check the answers and I am glad to see that I am indeed learning well. Had to get a piece of paper and make sense of it all with diagrams. You're all correct, I used wireshark again to see what was being exchanged between the proprietary client and the server, and I saw that the server would initiate a connection only after the client sent a specific handshaking message (none of that documented). This made me realize the client/server setup of both applications, and the very poor design of the developer, considering that there is really no need for a second TCP connection.

Again, thank you for the prompt solutions,


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 21 Jul 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100