I am new to C Socket Programming. I know how to write for TCP and UDP as different programs.
But only one server should handle both the clients.
Can anyone tell me how to write a sever that handles both TCP and UDP clients?
You cannot listen to TCP and UDP clients using 1 server socket. You can however create 2 server sockets (one TCP-server and one UDP-server). Note that it would not even make sense to have 1 server for both: UDP is connectionless so the first question arises when you try to do accept on your server socket (since it is a hypothetical hybrid version, what should accept do?).
Anyway, I assume you want two servers in the same event loop (if you don't know what it is, it is enough for you to think of it as your main-function). Since C sockets are blocking by default, you cannot run two servers right out of the box.
You can use select (Google it). If you don't know what it is, I would recommend to try it in Python first. In Python it's fairly straight forward and it will give you some insight of the concept. Basically what you do is: create multiple server sockets than "switch" between those sockets, see what sockets have read events (be it new connections or messages) and then process those events.
I can recommend libuv. It is a C library that is originally built for Node.js. Prior to libuv, they used platform-dependent event loop implementations (libev). Libuv originated as an effort to create a multi-platform library for non-blocking IO (TCP, UDP, fs, etc.). However, even if you don't want to write multi-platform code, it comes with a great API to create server sockets and listen to multiple sockets in the same event loop.
Related
I'm making a server/client program in C (LINUX). I have 4 programs in a folder. A clientUNIX, a clientTCP, a serverUNIX and a serverTCP. They all work flawlessly. Now, my goal is to make a server that supports both clients.
The easiest way of doing this, for me, was to start a new program (serverTCPUNIX) that does the following:
In main(), create a thread to handle TCP clients and another thread to handle UNIX clients.
Is there a better way of achieving this? Because this way, I'd have 2 threads looping through clients. I want to know if I can have only 1 thread and 1 loop that supports both types of clients.
Thanks.
Maybe you could switch the sockets on the server to non-blocking mode and then use select() to wait for either of them to receive a connection and handle it as needed, for example by creating a thread handling the client's request, and then going back to same select() to wait for the next incoming connection.
I am currently starting to learn to program sockets with the C programming language. I was wondering in the situation of a server that receives multiple messages, let's say I want to transfer a file from the client to the server. My question in this scenario is, do I have to call multiple times the accept function before calling each time the recv function? Or can I simply accept once the connection from the client side once and call multiple times recv?
Assuming that you are using TCP connections ("socket programming" covers a wide variety of topics, but TCP is the most common) accept completes the connection via sockets between your application and a counterparty and sets up a different socket that should be used to transfer data. The actual data transfer happens on this new socket. So only call accept once per connection.
See this page for a detailed description of what accept does.
I'm building a client-server program that has the client and server connected using a single TCP connection.
Within the communication there are several data "channels", which I want to multiplex over my single TCP connection, while still having good flow-control between the channels (preventing starvation and so on...). Also, tunneling will be nice, but not a requirement.
I'm using C, Windows.
I thought of using ssh but I have several problems with it:
I had trouble finding a nice open-source ssh code for windows.
Most of the code in SSH handles security, which I have no need for.
SSH seems a little too complicated for my needs, with all the X11, pty, shells and so on.
It sounds like you'd want to send data in "segments", where each segment would have a header giving the channel and number of bytes of data to follow. That way, you could intersperse data for the various channels on a single connection. Does that help?
I am writing a C program where for every new request to be processed by server, I need to open a new TCP connection? That is every request from client needs to be handled by a separate TCP connection to a server listening on a particular port.
Can someone help me in code pointers?
How to maintain array of this socket identifiers (multiple sockets that needs to be opened)
How will I be able to read (Need to scroll through all open sockets and see if something interesting is to be read upon that socket)
Any code snippet will be highly useful?
You can use the select() function (assuming you are working with <sys/socket.h>), "gives you the power to monitor several sockets at the same time. It'll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions" from http://beej.us/guide/bgnet/ (here you can download a pretty good book on network programming basics).
For a server example using select check http://beej.us/guide/bgnet/examples/selectserver.c
Hope it helps
If you need to save state with the socket, put it in a struct together with the data you need, and link the structures together as a list. For checking which sockets are ready see Maluchi's answer.
I wrote a program that creates a TCP and UDP socket in C and starts both servers up. The goal of the application is to monitor requests over the TCP socket as to what UDP packets to send it (i.e. monitor for something like "0x01 0x02" and if I see it, then have the UDP server parse the payload, and forward it over to the TCP server for processing). The problem is, the UDP server will be busy keeping another device up, literally sending thousands of packets back and forth with this device. So what is the best way to continuously monitor requests from the TCP server, but send it certain payloads from the UDP server when requested since the UDP server will be busy?
I looked into pthreads with semaphores and/or mutex (not sure all the socket operations are thread safe, though, and if this is the right way to approach it) as well as fork / pipe. Forking the UDP server off as a child process seems easy enough, but I don't see exactly how I would be passing the kind of data I need among both servers (need request data from TCP and payload data from the UDP).
Firstly, would it make sense to put these two servers into one program? If so, you won't have to communicate between processes, and the whole logic becomes substantially easier. You will have to think about doing asynchronous input and output, and the select() function is designed for just this. There will be many explanations around on how to do this, and a quick look finds this page.
However, if you must have two separate processes, then you will need to choose a mechanism for inter-process communication, of which there are several, and your choice will be affected by your operating system. A pipe, if available, might be suitable, as might a Unix named pipe. Or you could look into third-party message passing frameworks, or just use shared memory and/or semaphores (but be very careful!).
What you should look at is libevent, anything else you are reinventing the wheel writing this low level code yourself. Here is a Tutorial, Google, Krugle
Also you should use some predefined protocol between the servers. There are lots to choose from. Ranging from the extremely simple XDR to Protocol Buffers.
You could use pipes on Unix. See http://tldp.org/LDP/lpg/node11.html
Well, you certainly picked an interesting introduction to C!
You might try shared memory. What OS?