Click to See Complete Forum and Search --> : Advice on how I should approach this, newbie question


voidflux
July 4th, 2007, 08:33 PM
Hello everyone,

I'm new to network programming and I was just wondering if I could get some clarification and some advice on networking.


Right now I have a serversocket program that accepts a connection from another server that is sending large strings (events), my server socket program takes these strings (events) and parses them and up stores them into an object and then into a multi-map.

Now I'm going to take these events and turn them into a string again, and send them to a receiver.

I'm confused on the processes of how this will happen.

My Sever Socket program creates a server socket, and listens on port 5656, the program thats sending me the strings (events) connects to socket 5656, and starts sending me data.

Now once this data is processed and stored as a message object, then what do I have to do to send it off to another program?

Do I open up another Sever Socket, and listen on port 5000, then when that other reciever program connects to 5000 it will start sending those strings to it?

If this is the case, what if there is no connection to 5000 to recieve the strings I'm going to send to it? Would I need to maybe store all the strings in a vector and when the client connects to port 5000, should I just start sending them from the vector?

Or should the recieving application be also a server? So as soon as I get my first string, I can just use the Socket class, rather than the ServerSocket to create a connection like: Socket echoSocket = new Socket("taranis", 5000); Asumming taranis is the name of the server that wants the strings I parsed.

So my application will have a SocketServer to accept the incoming string, then it will create a Socket and connect to another server to send the string?

Thanks :D

MikeAThon
July 5th, 2007, 12:13 PM
Do I open up another Sever Socket, and listen on port 5000, then when that other reciever program connects to 5000 it will start sending those strings to it?
Not exactly. You have two choices.

First, if the other receiver program is already connected to the server program, then simply use the existing connection.

If not, then your server program must open a new socket, but it should not bind to a specifically-numbered port. Use IN_ADDR_ANY (sp??), and let the system pick a port for you.

The reason is that in this situation, the server program is acting like a client. A client should not specify his port. Instead, the client lets the system pick a semi-random port number, which is called an "ephemeral" port.

The server program now calls connect() to connect to the other receiver program. This means that the other receiver program is actually acting like a server, so it must have already opened a socket that listens for incoming connections on a pre-agreed-upon port. The server program must call connect() to that port.

Mike