简体   繁体   中英

Passing control information between client-server in socket programming in C

I am coding for a server-client programming in C using socket API where I am trying to send control information to client for using different TCP connection. Whenever the server has created new socket(TCP), I want it to notify the client to use the new socket for further communication.Currently I have thought of sending UDP packet to the client to be notified. Once the client receives the packet, it sends back the ACK to the server and at the same time switches to the another TCP connection. I want to know, is there a better to communicate the control data across the network besides using UDP as it is unreliable.Thnx

I would like elaborate on what I am trying to achieve. I am going to measure the parameters like bandwidth,latency, receive window etc. as metric for a Ipv4 and IPv6 TCP connection. Based on the observed performance I will be switching between the two protocols whoever provides better performance.Once the decision is made to switch , I have to notify the partner(may be client or server based on type of bandwidth I am measuring upload, download). I start with IPv4 connection and at the same time open another connection - IPv6 for measuring the bandwidth and latency.

If the IPv6 connection provides better performance, I need to tell the client to switch to IPv6. In this case both the connections are open for monitoring the bandwidth periodically to decide on switching. So I have two queries on this aspect. 1.Is it a good idea to keep two connections at a time.I can create the other connection only when I need to measure the metric as the path followed between two machines will hardly change.If yes, I can pass the control information using the other TCP connection to tell the client to switch. In this way I can also measure the bandwidth and notify client

It it is not a good idea two have two TCP connections, I can use UDP to send the control information. I am avoiding to send control information on the conn used to transfer actual data as it will be an overhead. My code will work like a middleware for transfer the data, where the application will call my functions/macros to transfer the data, and the internal code will take care of measuring the bandwidth and decision to switch.I hope I am clear on what I want achieve. Thank for your feedback in advance

The normal sequence of operations for a server-side listening TCP socket is:

int fd1 = socket(...);  // Create socket - assuming TCP, not UDP
bind(fd1, ...);         // Bind it to a local address
listen(fd1, ...);       // Set it in listen mode

int fd2;

while ((fd2 = accept(fd1, ...)) != -1)  // Accept an incoming connection
{
    ...communicate with client via fd2...
    close(fd2);         // When finished
}

close(fd1);

Now, at the point when you get fd2 , that has a socket with a different local ephemeral port from the port that fd1 is connected to (correction per EJP ). There is no need to communicate any specific information back to the client; the TCP/IP implementation deals with that for you. The client will have a socket that is connected to fd2 , with a port (probably an ephemeral port) on its machine connected to the server and the server's ephemeral port.

The point is that the completed connection will have a unique 4-tuple of (client IP address, client port, server IP address, server port).

When it comes to the processing, there are various ways of dealing with it. You can use an iterative server which deals with one request before dealing with any others. Or you can create a concurrent server in either of two different ways. One way is with a threaded server where a thread (from a pool, or newly created for each incoming connection) deals with the new request while the main thread goes back to accept new incoming requests. The alternative is that you create a forking server where the main server process forks and the child process deals with the new connection while the parent process closes the connection and goes back to listening for the next connection request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM