• C

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 :(
LVL 10
mstrelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Beej's guide is excellent, and you should find all information you need in there.

The examples are for one port only, yes, but having two ports listening isn't very different. You just do the same thing for the second port.
0
sunnycoderCommented:
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
0
Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

sunnycoderCommented:
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)
0
Infinity08Commented:
@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.
0
mstrelanAuthor Commented:
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?
0
mstrelanAuthor Commented:
@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
0
mstrelanAuthor Commented:
ps i dont care what port is used on the client side, just the ports used on the server
0
sunnycoderCommented:
>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.
0
mstrelanAuthor Commented:
"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."
0
mstrelanAuthor Commented:
"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."
0
sunnycoderCommented:
>>if so how can i find out what  the new port number is?
netstat command - works on windows and *nix - would help you see all network connections

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.

The problem statement does not require two ports. You can handle them with a single port. Follow Beej's guide and get a single threaded server working. Multithreading it would not be very hard - all you would need to do is to hand off the new_fd to a thread for further communication. Implementation of thread pool is a different beast altogether ;-)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mstrelanAuthor Commented:
"Upon acceptance, the server gets a new socket bound to a different port."

emphasis on the word different
0
sunnycoderCommented:
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.
0
mstrelanAuthor Commented:
the source of this is a word document from my lecturer
0
mstrelanAuthor Commented:
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 ;)
0
Infinity08Commented:
This part of the problem statement :

>> It needs a new socket (and consequently a different port number)

is wrong. Sockets can exist for the same local port, as long as the remote ip/port is different.
Or in other words, a socket refers to 4 values :

        (a) the local IP address
        (b) the local port
        (c) the remote IP address
        (d) the remote port

Together, these 4 values describe the connection. No two sockets can exist with all 4 values identical. This means that if at least one of the 4 values are different, the two sockets can co-exist.


So, a new socket does NOT mean that it HAS to be for a different port.


Although the assignment says that the port needs to be different, I agree with sunnycoder that nothing in the assignment requires to have a different listening port.
0
mstrelanAuthor Commented:
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
0
Infinity08Commented:
>> 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 :)
0
mstrelanAuthor Commented:
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
0
Infinity08Commented:
>> 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.
0
Infinity08Commented:
>> 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.
0
mstrelanAuthor Commented:
ok me again - can someone please help with my next question
http://www.experts-exchange.com/Programming/Languages/C/Q_23836204.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.