Solved

need tips for sockets and ports in c

Posted on 2008-10-22
23
222 Views
Last Modified: 2010-08-05
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 :(
0
Comment
Question by:mstrelan
  • 11
  • 6
  • 5
  • +1
23 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 22774494
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
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 22774498
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22774504
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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 350 total points
ID: 22774515
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22774534
@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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774546
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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774559
@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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774564
ps i dont care what port is used on the client side, just the ports used on the server
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22774584
>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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774590
"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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774594
"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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Accepted Solution

by:
sunnycoder earned 350 total points
ID: 22774621
>>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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774645
"Upon acceptance, the server gets a new socket bound to a different port."

emphasis on the word different
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22774655
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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774675
the source of this is a word document from my lecturer
0
 
LVL 10

Author Comment

by:mstrelan
ID: 22774704
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 150 total points
ID: 22774714
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
 
LVL 10

Author Closing Comment

by:mstrelan
ID: 31508665
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22774767
>> 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
 
LVL 10

Author Comment

by:mstrelan
ID: 22774902
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22775040
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 22775046
>> 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
 
LVL 10

Author Comment

by:mstrelan
ID: 22775090
ok me again - can someone please help with my next question
http://www.experts-exchange.com/Programming/Languages/C/Q_23836204.html
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now