Hi Guys I need to write a chat server in C. It only needs to use IPC.
Could you help me on how to proceed on this. A skeleton code will help me a lot.
Write an echo server: a server that accepts one client, and repeats everything the client says back to it.
Expand this server to support multiple simultaneous connections.
Have the server echo to all connections.
Consider as commands some pattern of lines from clients -- an initial "/", say, and act on them (close the connection, name the connection, list connections, etc.) rather than echo them.
Prefix all echo'd text with the name of the client, with a default "Anonymous$N" and then the name set by a command from #4.
When receiving a new connection, have the server elicit a name from it before the server begins echoing text from it and acting on other commands.
And so on. As mentioned, Beej's Guide can help you get past #1 and #2.
EDIT: OK, you added the 'IPC' language. You can still use sockets for this over the loopback device, unless you've some special requirement that you think IPC covers. You can also use UNIX domain sockets - named pipes. perlipc discusses them with a short example, and you can continue to e.g. the GNU C library manual.
Related
I have to develop 1 FTP (simple) server in C.
What do i mean with simple??
I mean that FTP server has to support:
authentication (USER, PASS, PORT)
change directory (CD)
file listing (LIST)
file retrive (RETR)
I have just developed as "split part" the numbers 1-2-3-4 and i have to merge them into main.c. So no problem with them.
What i don't understand is how to "receive" communications from the client.I mean how can implement what the client wants (USER, PASS, CD, LIST, RETR)?
I've read the rfc959 but i have not totally understood the communication between client and sever.
Summary:
client connect to server (so into the server this part is the socket "accept(..)"), rigth?
1a server send to client "you are connected" right?
client send to server user,password (file 1, auth.c but i don't know how to read when client send to the server the user and password request)
after successful login the server send file list (part 3, list.c, no problem)
the client send to the server the file it wants to download or the changedir command (again, how to read this info from client's request?)
i hope you have understand :)
EDIT: i'm on GNU/Linux
Although your question is little confusing, I am trying to answer it. Take command line parameters like "Username Password" and pass these argv[] through send to Server and receive using recv at Server end. For better understanding, refer the link Client.cpp
Hope this solves your confusion.
If you're using the Berkeley sockets API (your mentioning accept() implies that you are), you typically use recv() to read data from a socket. You need to read from the socket the client is connecting to, then inspect what you got to determine if it's a valid command that you can handle.
I was looking for a code to learn the workings of ftp with sockets and found filework on google code and the code clear and simple.
URL: http://code.google.com/p/ifilework
First of all, the environment I'm working on is Windows 7 and Visual Studio 2010.
I already wrote a server that uses the select method to retrieve data from more than one client.
Also I wrote a client that connected to the server above, by running (client.exe localhost 4444 Peter). "Peter" is the username that this user wants to use.
Now let's say we have two users connected on the server. Each of them has the ability to run the command /help. This returns some other commands that the user can use. One command of these is /listusr that returns all the users on the server.
One other command is the /talk2 and here is where my problem-question begins. I want to let the user choose to which of the other users want to talk. E.g if you want to talk to Peter, you give /talk2 Peter.
How am I going to start something like this? How will the server send the message from me to Peter (I have to add here that when a new user connects, the server saves his/her username and his/her socket number in a struct)?
Do I need to create new threads for each conversation or new processes? Can someone give me some hint or advice to continue my project? I'm little confused on how to manage at this point.
Neither. Your server should maintain some kind of data structure that matches a user id to a client socket handle. When a request comes in with the /talk2 command, the server should look up the corresponding socket handle for that userid and should simply relay that message using send().
A scalable way would be your sever is just responsible to tell both clients the IP address of the other side, and then Peter and you establish the connection so you can talk.
If you really want to have the sever transfer the conversation, you need to consider the following to gain a better scalability:
Use UDP instead of TCP
Use thread instead of process
Spawning a new process would be an ordeal for the server if the number of users interacting at a time are high. But on the other hand it will be simpler to code.
Threads do provide scalability, but then you must be extra careful in your code not to do anything silly. (For example, sending wrong chats to the wrong guys.)
Use select/poll techniques (I am not sure how they perform in the Windows environment, but it works cool with Linux.)
UDP will reduce transmission time, but I am not too sure if it's a good idea. Since you said you already have a code, it would be great pain to switch to UDP.
Just sending the address of required client is also a feasible idea. It reduces a lot of effort from the server, but now you will require dedicated clients.
Try each of them and check which one works best for you. It's a design problem, so there can't be a hard and fast solution. It will depend on the usage of your application. You may also want to use (may be you are already using) the sendto and recvfrom functions.
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 want to implement a Unix daemon (let's call it myUnixd), and want the user to be able to interact with this daemon via the command line, for example:
myUnixd --help # will display help information
myUnixd --show # will show some data (the's deamon should be doing the work)
So my question is: How can I communicate with the daemon? I was thinking about Unix domain sockets. Can someone tell me the right way to do this?
Thanks.
Use Berkeley sockets. Specifically, you can create a "UNIX domain socket" (otherwise known as a "local domain socket," which will create what looks like a text file. Write to the text file to send text to the daemon, read from it to receive text from the daemon. You can implement this with a few function calls.
If you want something more advanced, you can also use DBus, which offers a more sophisticated interface, but which is more complicated to learn.
use tcp socket if you want to use telnet to communicate with your daemon.
One could also use Remote Procedure Call (RPC) for such client-server communication. There are different types of messages (protocols) that can be used together with it, one of them is JSON.
The JSON-RPC protocol is very well accepted for such tasks. You can find different tools and libraries to embed in your software. A quick search on google gives this C library. The advantage of such libraries is that from a JSON specification file, where you define all your remote function calls, it creates client and/or server stubs that you can just use in your code out of the box.
As a listener one can use sockets, as the other responses state, or just an embedded HTTP server like microhttpd (and libcurl for the client). There are plenty of examples out there to just reuse. HTTP allows you also to run your client behind a proxy.
On a server, a process monitors the files in a Unix file system.
If a client sends the file name to be monitored, the server has to send the report to the client whether that file got changed or deleted.
For server-client communication, we should use either message queues or sockets.
For every change in the file, the server has to notify that change.
For all changes in all files, the server has to maintain one logfile so that user can view it through the command line interface.
Server could use two threads: one for communication, the other for monitoring.
How can you do this efficiently?
Have a look at inotify at http://en.wikipedia.org/wiki/Inotify - linux only I'm afraid.
BSD has kqueue - see http://en.wikipedia.org/wiki/Kqueue.
See also:
http://benkibbey.wordpress.com/bubblegum/