I have a problem in this question, please anyone can guide me
Communication between client and server in C ubantu:
Client will send message and address of another client to server and server will forward that message to that client. Other client will reply to server in a same format and server will forward that message to specified client.
I have to use fork command in this Question.
How I will send address to server and message together?
I don't think you should use fork in this case. You can keep an array containing your client's sockets with a unique ID, which can be the address, and forward the message you receive from a client to the one with the ID specified in the original message. The select() system call can be useful when monitoring an array of file descriptors.
Related
I will try to express it as good as a can. I need to create an online chat but NOT a group chat, but a private one.I mean i need a server where a lot of client can connect and each one of them can send a message to another client(if he knows his IP or Username). If the receiver is offline he will receive the message when he will connect to the serve(the easy part). If he is online he could do 2 jobs:
1.Send message to another client
2.Receive a message from another client.
My problem is how to interupt the sending process and tell the client that he has a new unread message?
I am looking up to use the SIGIO signal,but i am really not familiar.Am i in the right way?
PS:The server is running in an embedded system.
I have a client-server programm. They communicate with some characters.
ex:
client --send A-> server
then
client <-recv A'-- server
But I let server send message back not in order.
ex:
client --A--> --B--> --C--> server
then
client <--A'-- <--C'-- <--B'-- server
what I want:
client <--A'-- <--B'-- <--C'-- server
so I want to handle the situation in client programm.
I only figure out one way is to keep a buffer to record data from server, and client will check the B' received and then check the C' received in order.
Is there anyway to do that in client and avoid using buffer?
Ideally you should send and then wait for reply, send next request when you received earlier reply.
But apparently your client seems to send without waiting for reply and server replies whichever request its done processing.
In such case you need to device a mechanism in your client and server program based on the data being sent and received.
You can decide your client and server will work on data having(Header+data) format something like:
length(2 bytes) - length of actual data
sequence numner(2 bytes) - to be incremented for each request
Actual data(length number of bytes)
When you send the data keep the request data, after sending over socket, in a pending list of requests waiting for reply from server.
The server on receiving request in above format, will update its reply data in the 'Actual data' section , update the length in header, keeping sequence as it is, server will send reply to client.
Client will match the sequence number from reply with the items in its pending list to get the request whose reply it received from server
I'm implementing in c a sort of FTP protocol.
I' ve a server running.
I start the client, connect to the server, and then send a GET file.txt request.
The client parse the command, see it's a GET command and starts a server socket.
The server recieves the command, and starts the data connection with the client and start sending file.txt on this connection.
When the server sent the file, it closes the client socket (Data).
When i want to GET another file, the port is already in use. How can i prevent this? Should i keep the data-connection open for all the command-connection session? In this case, how can my client know when the file is over?
Thanks
When a socket is closed, it enters the TIME WAIT state (see here for the possible TCP states) and no other socket can be bound to the same address/port pair until the socket leaves TIME WAIT and is in the CLOSED state.
You can go around this by setting the SO_REUSEADDR socket option, that will allow two sockets to be bound to the same address if one of the sockets is in the TIME WAIT state.
you need to open socket for transfer each time as the server will close it when transfer finish.
you will know that the file is downloaded/uploaded by reading response from FTP Server for status code (226 or 250) - check List of FTP server return codes:
https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
In my project, I use apache-commons-net,
just keep the command connection alive with heartbeat command,
and enter local passive mode every time to do file tranfer.
The principle is same for your situation, I suggest send
EPSV
command before GETTING a file.txt.
refer: https://commons.apache.org/proper/commons-net/
I am trying to design a Client Server kind of application in which my Server is a daemon that accepts client requests, send client's data over a serial channel to the other side(which is an MCU and its firmware will reply to the Server request over the same serial channel). My client can be a CLI application or any other system program.
My idea of design is -
Use message queues for communication between Client and Server since this is a local application and message queues are bidirectional and fast.
Implement a LIBRARY that acts as an interface between multiple clients and the server. This basically does the stuff of packetizing client data into a message(own defined protocol), create message queues, connect to server, send/receive data and then pass it to the respective client(using call backs). This library also exposes API that can be used by clients. Thus this library gives me the flexibility to add support for any new clients keeping the server program unchanged.
Server gets the data over serial from other side and passes it to the library over message queue. The library uses callbacks to send data to the client.
EDIT:
I am thinking of creating Message queues on the fly when any client requests arrive. If I do this, how does the Server daemon(which has already started at linux boot up) gets information about this message queue? Does the message queue has a name that is persistent across and used by other programs? I want to implement clients that will be blocked until it gets response from the server.
Could you guys please review this design and tell me whether my approach is correct. Please reply if you have any other recommendations.
Thanks in advance.
in a Silverlight application, we used to send requests from client to server and receive its response.
is it possible to send an information from server to client which is not waiting for? for example server ask client to update a text in a special text box and so on. "an info or known command which may come to client any time and is not a response to the client's request"?
You can use Duplex Services for this.