I have 2 files Client Side & Server Side
I send a string over the socket from client to server. I have to execute this string as one does in a terminal. The output of the command is to be displayed on the client side.
Server Side Code : this loop runs on each thread created by pthread_create
while((n=recv(sock,client_message,2000,0))>0)
{
send(sock,server_out,n,0);
}
I need to run the string i recieve in client_message as a terminal command and fetch the output of the command and send it back via the server_out string buffer.
How do i go about this ?
So - you have two or three different tasks to accomplish.
The first one is to run the command line you received on the server. For that, you can start reading the system() function. It's very straightforward to use.
But then you need to get it's output. You can read about this two points in this question.
Lastly, send that data back to the server - once you have the output stream, it's just send()ing that via the socket. You can implement some mini-protocol for telling the other side how many bytes to expect, some error detection/correction if you want, etc.
Once the data arrives the client, then you can do whatever you want with it - print it on the screen, save to a file, you name it.
Read about this things, take your chances, and come back to continue asking if you need it - good luck!
Related
I have seen there are some questions about this topic, but none of the answers satisfied me. Here is the problem: I need to write two sockets (client and server), with the client having to send to the server an awk program with some lines of input. No problem in sending strings back and forth between the sockets. Supposing I have stored the program in a string command and the string I should pass to it in input, I have tried this:
execl("/usr/bin/awk", command, input, (char *)0);
And this works, the awk program runs and it writes on the server's stdout and stderr. Thing is, if there are lines with errors, I need to send these back to the client, which is pretty impossible since execl doesn't give me the chance to store its output in arrays. So, does anybody know a way to do this without using system and popen?
I'm trying to build a python client to interact with my C server. Here's the code for the client:
import socket
s = socket.socket()
s.connect(("127.0.0.1", 12209))
print "preparing to send"
s.send("2")
s.send("mmm2.com")
s.send("mypwd")
s.send("5120")
print "Sent data"
root = s.recv(256)
print root
When I run this code on the interactive shell (the GUI IDLE) of course line by line, everything runs very fine. But when i save this code in a file and try to run it, it hangs and stops responding according to windows, what's it that I'm just not doing?
If you type it line by line, the sent strings are likely received by the server one after another in separate recv() calls.
When you execute it in a script, all the send() calls run immediately after each other without delay and the server will probably receive all the data in one bulk in a single recv() call. So the server will see "2mmm2.commypwd5120", and maybe not handle that correctly. It might wait for more input from the client.
You will need some explicit separation between the values, for example newline characters, so that the server can parse the received data correctly.
I'm trying to write a chat client and server and the client should be able to print the messeges from the server while also writing something on the console.
So I created a pthread that should read the user input and the main thread prints the messeges from the server. But when I type something and while typing a messege is received, the text I was typing is pushed up on the console. How can i fix that?
for example:
I'm typing "abcdef" and then when I get a messege from the server (but didn't finish typing yet) it will look like this:
abcdef[Chatuser1]:Hello
Use synchronization technique like semaphore or mutex to synchronize input and output in your multi threaded program
I would really recommend doing something like this in a GUI with two seperate edit box elements and not in a console.
If you still want to do it in a console, you will need to do direct console buffer modification.
Every time a new output message arrives, the current input message has to be backed up so you can savely write to the console then do some custom scrolling and add the input message back again. Thread syncronisation is needed to prevent mixing of input and output in the buffer.
I think you also would not get around custom key handling, because otherwise you have no access to partialy typed input.
I'm doing a lab assignment where we make a Server program and a Client program. Its on the QNX OS. Not sure if it runs in Linux. The outline is this:
"Write a pair of C programs msgSender.c and msgLogger.c to demonstrate Neutrino message passing between processes.
Your programs will be called from the shell as:
$ msgLogger logFileName
$ msgSender msgLogger
logFileName is the name of the log that stores the messages
The msgLogger process acts as a logger. It receives messages and writes the messages to a file.
msgLogger receives text-based messages of the format shown in msg.h. It must test the message header and only write message text to the logFile if the message type is MSG_DATA.
If MSG_DATA is received, the reply status is MSG_OK.
If a MSG_END is received, the server replies with a status of MSG_END and then cleans up and exits.
If the received message is not MSG_DATA or MSG_END, the reply status is MSG_INVALID and the message text is not logged. A warning message is logged.
This process advertises its presence by writing its ND PID CHID to a file named msgLogger.pid where the "msgLogger" part of the filename is taken from argv[0].
The logged messages are stamped with the time and the ND PID COID of the sender.
The msgSender is an interactive program that assembles and sends the text-based message.
Reads the name of the logger process from the command line and uses this name to build the name of the .pid file where it reads the ND PID CHID.
It prompts the user for the message header type and then for the text of the message.
It will exit if it receives a MSG_END from the server.
It prints a warning if MSG_INVALID is received from the server
Your client and server must interoperate with my client and server.
Validate that the server works properly with multiple concurrent clients.
If you flush your server's file write buffer after each logging message, you can run it in the background and use
$ tail -f logFile
to view the messages as they are received.
Be sure to check the validity of the command-line argument.
Use global variables only when necessary.
"
I've got the msgLogger fully working; here is the code:
http://pastebin.com/8AGfGZ5u
And here is the msg.h file:
http://pastebin.com/3xcBZvnH
And here is the code I have so far for msgSender:
http://pastebin.com/Buk88Kry
What the sender (client) needs to do, is let the user enter the message type using digits. The msg.h file contains the type of message numbers with MSG_DATA being 1, etc. If they enter an invalid digit, it'll ask them to try again, else it will store that digit and assign it to the amsg.m_hdr of the MESSAGE struct. amsg.m_data is the value with the message.
Then the user enters the message they want, and if they chose the digit 1 (msg_data), the server sends notification and the client prints "message successfully received", while the contents of the message are saved to the log file.
Unfortunately I'm having a bunch of problems and it's not logging the message. I have to hand in the msgSender tomorrow, and it's also dependent on my next lab. I really hope I can get some help on this.
Try flushing the buffer after the client writes. If you don't close the file descriptor while writing to the file, there's no gaurentee that the buffer was flushed and the file was written. You can ensure that all your writes sync to the file whne you want by calling fsync().
I'm trying to develop a traceroute server which accepts argument from a telnet client performs traceroute and send the information back to the telnet client. I'm receiving the argument from telnet client at the server for e.g. traceroute www.google.com but somehow when I try to execute it, it gives me a weird error:
: Name or service not known
' on position 1 (argc 1)line arg `www.google.com
What I found strange is when I hard code the command at the server end it works fine, also when I receive the command at the server end and print it out, that works fine too. However, the same command received in a character array it fails to execute with the above error.
Here's my code:
int main() {
int sockfd,new_fd;
char client_arg[100];
//Create a socket and establish the connection
if (recv(new_fd,client_arg,100,0)== -1)
perror("recv");
printf("%s\n",client_arg); // prints traceroute www.google.com
system(client_arg);
return 0;
}
Many things could be wrong here:
traceroute might not be in the default PATH of /bin/sh,
the string might not be zero-terminated,
the buffer might have some unprintable characters that printf would not show to you,
you might receive a short read with partial command,
anything else ...
It is a very Bad IdeaTM to blindly pass user (or network) input for execution like that. Do explicit checking. Pass only allowed commands. Check correctness of the arguments.