Link to home
Start Free TrialLog in
Avatar of mstrelan
mstrelanFlag for Australia

asked on

need tips for sockets and ports in c

I'm getting a little desperate - I need to create a Server/Client app in C. The server needs to listen on a certain port for incoming connections. It then needs to open a new port (not just a new socket) and the client needs to communicate on the new port. I need to make the server threaded and allow multiple connections but lets start with the basics... any tips / links to tutorials etc? im currently using http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#select as a basis but for my assignment i need to use different port numbers :(
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Once a connection has been accept()ed further communication indeed happens on a new port. You are using the best online tutorial - stick with it.
http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#accept
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@sunnycoder : I think what mstrelan needs is two connections : one control connection and one data connection, just like in the FTP protocol. Correct me if I'm wrong, mstrelan.
Avatar of mstrelan

ASKER

wow thanks for the quick response guys... sounds like im more on track than i thought

@sunnycoder

>   new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
>   accept returns you a new socket fd over which you continue further communication (send/recv)

are you saying that this new_fd is on a different port number? if so how can i find out what  the new port number is?
@Infinity08

To be honest I don't fully understand what I need. This is what I know - the Client must connect on a particular port, the server must be able to listen for multiple connections. Once the Client has connected it needs to communicate on a unique port that no other client can use
ps i dont care what port is used on the client side, just the ports used on the server
>are you saying that this new_fd is on a different port number? if so how can i find out what  the new port number is?
Its a different socket id bound to the same port. Typically that is what you want for client server communication. It does not make sense to connect to one port and talk on another unless you need an entirely separate channel as I08 interpreted. Also latter is atypical of beginner's socket programming assignment.
If you can post the exact problem statement, we can help you with learning what you need to do.
"On the client end, the client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to reach the server on the server's machine and the specific port.

If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while dealing with the needs of the connected client."
"Most of the time, a server would serve not only one client on the network. Its quite possible that up to hundreds of clients are trying to connect to the server at the same time. One way to handle requests from more than one client is to make the server program multi-threaded. Using threads, a multi-threaded server program can accept a connection from a client, start a thread to serve that client, and continue listening for requests from other clients

Even further, we may create a thread pool upon running the server program. A thread pool is a collection of threads that can be used to perform a number of tasks.  The thread pool in this application contains some fixed number of threads to be invoked. When there is no connection from clients, they just wait in the pool. Once any connection is accepted, one of the threads will be woken up to handle the communication with one client. This should be implemented in this assignment."
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"Upon acceptance, the server gets a new socket bound to a different port."

emphasis on the word different
http:#22774590 what is the source for this? Open your browser and connect to any site (you must be connected to EE if you are reading this :-) ... run netstat -an ... you would see connections to port 80. If this were true, you would be connected to some other port.

Was in response to that.
the source of this is a word document from my lecturer
ok I'm going to forget about opening new port numbers and work on the threading. expect some more questions about threading over the next few hours ;)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't know where I'd be without you guys sunnycoder and Infinity08. Seems like you guys just sit and answer C questions all day, I always get a response in < 5 mins. thanks heaps
>> Seems like you guys just sit and answer C questions all day, I always get a response in < 5 mins. thanks heaps

Heh ... Boredom helps with that lol. Glad to be of assistance :)
i just spoke to my lecturer and she seems to think its not possible to have multiple connections on the same port... but i trust you guys more than her so hopefully she can learn a thing or two
>> she seems to think its not possible to have multiple connections on the same port

Then how would a web server work for example ? All requests to a web server arrive at port 80. If only one connection would be allowed at a given time, then that would mean that each person that wants to connect to the web server will have to wait until the previous person's connection has been closed.
Luckily, that's not how things work, and servers can accept multiple concurrent connections on the same port.
>> and servers can accept multiple concurrent connections on the same port.

Note that the server will have a separate socket for each of those connections. But all of them will be for the same port.