Basically my program is suppose to be able to have multiple connections to a server at the same time. Which I have running, except when they have to send large amounts of text via a socket, then it is unpredictable. Sometimes it works, sometimes not. The plaintext4 file is 69,333 bytes long, when I try to send it 5 times at the same time over the network via a socket to a server and back, it doesn't work always; sometimes it works, sometimes parts of it is missing, etc. When I used basically the same thing using write and read it worked, but when I tried to the same with large amounts of text, it didn't work almost at all, hence why I switched to send and recv. Now I can't figure out how to make it so when someone is sending sometime, no one else will send at the same time... because I think that is the problem I am having. Any help would great.
The status of my program right now:
- Works when sending small amounts of text.
- Doesn't work when sending large amounts of text.
- The size of the file that it creates when it doesn't work can vary, sometimes is above 60K bytes sometimes as little as 20K bytes. Meaning that probably one of the other processes wrote to the socket or read from it before the correct process got to receive it. (I think).
server code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
const int BUFF_SIZE = 70000;
int sockfd, newsockfd, portno, newsockfdc;
socklen_t clilen;
char buffer[BUFF_SIZE];
pid_t pid;
int status;
char temp[BUFF_SIZE];
struct sockaddr_in serv_addr, cli_addr;
int n;
int i;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
while(1){
waitpid(-1, &status, WNOHANG);
memset(buffer, '\0', sizeof(buffer));
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
if((pid=fork())==0)
{
close(sockfd);
int d;
for(i=0; i<1; i++)
{
bzero(buffer,BUFF_SIZE);
//sleep(1);
d=BUFF_SIZE-1;
//n = read(newsockfd,buffer,d);
n = recv(newsockfd, buffer, sizeof(buffer), 0);
if (n < 0) error("ERROR reading from socket");
bzero(temp, BUFF_SIZE);
strcpy(temp, buffer);
n = send(newsockfd, buffer, strlen(buffer)+1, 0);
//n = write(newsockfd,temp,sizeof(temp));
if (n < 0) error("ERROR writing to socket");
}
close(newsockfd);
exit(0);
}
else{
waitpid(-1, &status, WNOHANG);
close(newsockfd);
}
}
close(sockfd);
return 0;
}
client code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
const int BUFF_SIZE = 70000;
char buffer[BUFF_SIZE];
if (argc < 4) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
int numt;
numt = atoi(argv[3]);
char numc[5];
bzero(numc, 5);
sprintf(numc, "%d", numt);
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
bzero(buffer,BUFF_SIZE);
FILE *fp;
size_t bytes_read;
if((fp=fopen("./plaintext4", "r+"))==NULL)
{
perror("fopen(2) file error");
exit(EXIT_FAILURE);
}
bytes_read = fread(buffer, sizeof(buffer), 1, fp);
fclose(fp);
sleep(1);
n=send(sockfd, buffer, strlen(buffer)+1, 0);
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,BUFF_SIZE);
n=BUFF_SIZE - 1;
n = recv(sockfd, buffer, sizeof(buffer), 0);
if (n < 0)
error("ERROR reading from socket");
char filei[90];
bzero(filei,90);
strcpy(filei, "plaintext");
strcat(filei, numc);
strcat(filei, "_a");
if((fp=fopen(filei, "w+"))==NULL)
{
perror("fopen(2) file error");
exit(EXIT_FAILURE);
}
bytes_read = fwrite(buffer, strlen(buffer), 1, fp);
fclose(fp);
close(sockfd);
return 0;
//sleep(1);
printf("Sending message 1: plaintext1\n");
bzero(buffer,BUFF_SIZE);
if((fp=fopen("./plaintext1", "r+"))==NULL)
{
perror("fopen(2) file error");
exit(EXIT_FAILURE);
}
bytes_read = fread(buffer, sizeof(buffer), 1, fp);
fclose(fp);
n = send(sockfd, buffer, strlen(buffer)+1, 0);
//n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error ("ERROR writing to socket client side");
bzero(buffer, BUFF_SIZE);
n=BUFF_SIZE - 1;
n = recv(sockfd, buffer, sizeof(buffer), 0);
//n = read(sockfd, buffer, n);
if (n < 0)
error("ERROR reading from socket client side");
printf("message: %s\n", buffer);
strcat(filei, "b");
if((fp=fopen(filei, "w+"))==NULL)
{
perror("fopen(2) file error");
exit(EXIT_FAILURE);
}
bytes_read = fwrite(buffer, strlen(buffer), 1, fp);
fclose(fp);
close(sockfd);
return 0;
}
You server process can (which process accept() call) can be blocked by waitpid - it is can be cause of your problem.
Alternatively you can implement waitpid call via SIGCHLD signal handler and remove waitpid from main().
Best regards!
Related
Sending a normal .txt file works perfectly fine. But if I try to send a .wav file, the output file that gets generated is only a fraction of the size of the input file (and it doesn't play anything). I've tried pretty much everything I could find on Google, it might have something to do with not reading the .wav file correctly. But I'm reading and writing one character at a time, so I don't know why that should be a problem. Also, doing this over localhost. Any help would be greatly appreciated, thank you!
server.c
/* A simple server in the internet domain using TCP
The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER_SIZE 32
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, n;
socklen_t clilen, servlen;
char buffer[BUFFER_SIZE];
FILE *fp;
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
servlen = sizeof(serv_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer, BUFFER_SIZE);
n = read(newsockfd, buffer, BUFFER_SIZE);
if (n < 0) error("ERROR reading from socket");
printf("Received file name: %s\n", buffer);
fp = fopen(buffer, "rb");
if (fp == NULL)
printf("File open failed!\n");
else
printf("File successfully opened!\n");
while (1) {
bzero(buffer, BUFFER_SIZE);
char ch;
int i;
for (i = 0; i < BUFFER_SIZE; i++) {
ch = fgetc(fp);
buffer[i] = ch;
if (ch == EOF)
break;
}
n = write(newsockfd, buffer, BUFFER_SIZE);
if (n < 0)
error("ERROR writing to socket");
if (ch == EOF)
break;
}
printf("File sending complete...\n");
if (fp != NULL)
fclose(fp);
close(newsockfd);
close(sockfd);
return 0;
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFFER_SIZE 32
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[BUFFER_SIZE];
FILE *out;
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Enter the file name: ");
bzero(buffer, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE - 1, stdin);
buffer[strlen(buffer) - 1] = '\0';
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
out = fopen("out.wav", "wb");
while (1) {
bzero(buffer, BUFFER_SIZE);
int i, j;
n = read(sockfd, buffer, BUFFER_SIZE);
if (n < 0)
error("ERROR reading from socket");
char ch;
for (i = 0; i < BUFFER_SIZE; i++) {
ch = buffer[i];
if (ch == EOF)
break;
j = (int)ch;
fputc(j, out);
}
if (ch == EOF)
break;
}
printf("File write complete... You can now use the output file!!\n");
if (out != NULL)
fclose(out);
close(sockfd);
return 0;
}
To determine the end-of-transmission, you have to check the return value of read(), rather than checking for EOF in the read data.
I have also taken the liberty to restructure and simplify your code.
Consider the following (untested) changes:
server.c:
// ...
while (1) {
size_t num_read = fread(buffer, 1, BUFFER_SIZE, fp);
if (num_read == 0) // end of file.
break;
n = write(newsockfd, buffer, num_read);
if (n < 0) // Error
error("ERROR writing to socket");
else if (n == 0) // Could handle this too
break;
}
// ...
client.c:
// ...
while (1) {
n = read(sockfd, buffer, BUFFER_SIZE);
if (n < 0)
error("ERROR reading from socket");
else if (n == 0) // Socket closed. Transfer is complete (or borked)
break;
fwrite(buffer, 1, n, out); // Could check fwrite too.
}
// ...
I have two programs, one acting as a server and one acting as a client. They are supposed to act like you are connecting to remote system using ssh. The client sends a command and the server executes the command and prints the output to the server. Although my code does exactly that, there is a delay on the output after the first command. For example if the client sents date, the server will return the date. If the client sends date again it will print message was received but not the output. On the third input from client, the second date will be executed and print on the client Here is the message:date and so on. Any ideas would be really apreciated.
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX_PINS 1
void error(const char *msg){
perror(msg);
exit(1);
}
void exec_comm(int sock,int nerror,char buff[]){
dup2(sock,1);
dup2(nerror,2);
if(system(buff)==-1){
printf("command not found\n");
}
}
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
char buffer[256];
char* comm[20],*cbuff;
struct sockaddr_in serv_addr, cli_addr;
int n,i,done=0,correct=0;
char str[INET_ADDRSTRLEN];
char* args,*pins[MAX_PINS]={"1234"},pin[10];
FILE* fd;
int nerror;
if (argc < 2){
fprintf(stderr, "No port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
if (inet_ntop(AF_INET, &cli_addr.sin_addr, str, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Could not convert byte to address\n");
exit(1);
}
fprintf(stdout, "The client address is :%s\n", str);
while(1){
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
sscanf(buffer,"%s\n",buffer);
printf("The message that was read was:\t%s\n",buffer);
if (n < 0) error("ERROR reading from socket");
fprintf(fd,"%s\n",buffer);
if(strcmp(buffer,"exit\n")==0||strcmp(buffer,"exit")==0){
printf("Exiting...\n");
done=1;
break;
}
exec_comm(newsockfd,nerror,buffer);
printf("Here is the message: %s\n", buffer);
n = write(newsockfd, "message received", 17);
if (n < 0) error("ERROR writing to socket");
}
fclose(fd);
close(newsockfd);
close(sockfd);
return 0;
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg){
perror(msg);
exit(0);
}
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3){
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL){
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
n = write(sockfd, stdin, sizeof(int));
if (n < 0)
error("ERROR writing to socket");
do{
printf("Please enter the message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
if(strcmp(buffer,"exit\n")==0||strcmp(buffer,"exit")==0){
printf("Exiting...\n");
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
close(sockfd);
break;
}
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n", buffer);
}while(1);
return 0;
}
n = write(sockfd, buffer, strlen(buffer));
That's your problem right here. Calls to write do not result in individual distinguishable messages, one message per call. They result in a single homogenous stream (that's the "stream" in SOCK_STREAM) of bytes with no delimiters.
You have newlines that separate commands, which is nice and all, but read has no idea about newlines. It will just wait until the buffer is full, or enough time has passed, or whatever. You have no control over it.
You basically have 2 ways to fix this.
Read character by character (pass the length of 1) and stop as soon as you see a newline. Accumulate the characters in a buffer, then execute it.
Send the length of each message before the message itself, in a fixed-length record, so that the server can safely read the length and then use it to read the message itself.
I currently trying to create a program that you can connect from anywhere via the internet using sockets. I obviously want to execute commands and display the output to the client but also want to return anything that the client typed and if it was received, display message reveived. My problem is that even though it displays what was send, it doesn't execute commands. I have tryied to use gdb with break lines inide the while loop but they don't work as if the while loop is never executed.
Server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
char buffer[256];
char* comm[20],*cbuff;
struct sockaddr_in serv_addr, cli_addr;
int n,i;
char str[INET_ADDRSTRLEN];
char* args;
if (argc < 2)
{
fprintf(stderr, "No port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
for(;;){
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
if (inet_ntop(AF_INET, &cli_addr.sin_addr, str, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Could not convert byte to address\n");
exit(1);
}
fprintf(stdout, "The client address is :%s\n", str);
while(1){
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0) error("ERROR reading from socket");
if(buffer=="exit"){
printf("Exiting...\n");
break;
}
cbuff=strtok(buffer," ");
i=0;
while(cbuff!=NULL){
cbuff=strtok(NULL," ");
comm[i]=cbuff;
i++;
}
if ((pid=fork())==-1){
perror("fork");
}
if(pid!=0){
wait(NULL);
}
else{
dup2(newsockfd,1);
printf("executing...\n");
execvp(comm[0],comm);
perror("execvp");
exit(EXIT_FAILURE);
}
printf("Here is the message: %s\n", buffer);
n = write(newsockfd, "message received", 17);
if (n < 0) error("ERROR writing to socket");
}
break;
}
close(newsockfd);
close(sockfd);
return 0;
}
Client:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg){
perror(msg);
exit(0);
}
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3){
fprintf(stderr, "usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL){
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
do{
printf("Please enter the message: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
if(buffer=="exit"){
printf("Exiting...\n");
break;
}
n = write(sockfd, buffer, strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n", buffer);
}while(1);
return 0;
}
I am modifying the server code from here http://www.linuxhowtos.org/C_C++/socket.htm. I want to reset the server when the client disconnects, so it's ready the next time for the client. But instead when I close the client side, the server shuts down and nothing is written in the server log. Since I only want to handle one client at a time, I don't need to fork right? Otherwise the server works fine and responds to messages I've sent from a client I wrote in C#.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
FILE *fp;
double square(double x)
{
return x*x;
}
void error(const char *msg)
{
fprintf(fp, "%s", msg);
fprintf(fp, "%s", "\n");
}
int main(int argc, char *argv[])
{
fp = fopen("serverlog.txt", "a");
int sockfd;
int newsockfd;
int portno = 43635;
int clientIsConnected;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
while (1)
{
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
close(sockfd);
return 0;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
close(sockfd);
return 0;
}
if (listen(sockfd,5) < 0)
{
error("ERROR on listen");
close(sockfd);
return 0;
}
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
error("ERROR on accept");
close(newsockfd);
close(sockfd);
return 0;
}
clientIsConnected = 1;
while (clientIsConnected)
{
bzero(buffer, 256);
n = read(newsockfd, buffer, 255);
if (n < 0)
{
error("ERROR reading from socket");
clientIsConnected = 0;
break;
}
double x = atof(buffer);
double squared = square(x);
char response[256];
sprintf(response, "%f", squared);
n = write(newsockfd, response, strlen(response));
if (n < 0)
{
error("ERROR writing to socket");
clientIsConnected = 0;
break;
}
}
}
close(sockfd);
return 0;
}
It may be that your server stops because it received SIGPIPE. You can use
signal(SIGPIPE, SIG_IGN);
to keep your program going and you can handle the closed communication in your application code.
i am trying to create a simple tcp server-client. My programm works fine if client sends data to server but when server tries to answer back the whole connection hangs. I am using write()/read() functions .
Server code :
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdbool.h>
#define SIZE 700000000
///////////////////////////////////////////////////////////////////////////
int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
listen(sockfd, 10);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
bzero(buffer, 255);
int readbyte = 0;
do {
readbyte = read(newsockfd, buffer, 1024);
if (readbyte < 0)
{
perror("ERROR on reading socket!");
break;
}
if (readbyte > 0)
{
//printf("Here is the message: %s\n",buffer);
}
} while (readbyte > 0); /* until EOF */
int writen = write(newsockfd, "I received your message", strlen("I received your message") + 1);
if (writen<0){
perror("Error");
}
//debug();
return 0;
}
Client code:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
//////////////////////////////////////////////////////////////////
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int mode;
char *code;
char buffer[256];
char *uptr = buffer;
int i;
if (argc < 3) {
fprintf(stderr, "Use: %s [hostname] [port] [args]\n", argv[1]);
exit(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
int sock_et = connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr);
/* Now connect to the server */
if (sock_et < 0) {
perror("ERROR connecting");
exit(1);
}
/* some code... */
while (len > 0) {
int writen = write(sockfd, buf, len + 1);
if (writen < 0)
{
printf("Error writting to socket!\n");
break;
}
len -= writen;
buf += writen; /* tricky pointer arythmetic */
}
printf("Finished writing to server\n");
bzero(buffer, 256);
int readbyte;
do {
readbyte = read(sock_et, buffer, 1024);
if (readbyte < 0)
{
perror("ERROR on reading socket!");
break;
}
if (readbyte > 0)
{
//printf("Here is the message: %s\n",buffer);
}
} while (readbyte > 0); /* until EOF */
printf("%s\n",buffer );
close(sockfd);
return 0;
}
Any help would be appreciated! Thanks!
Your client code doesn't compile
You are never going to get a 0 (EOF) from read while the socket is open (in either the client or server) -- so just add a break after a successful read for now.
You are not closing the socket in the server
In the client, you need to read from sockfd, not sock_et
There are probably other issues. It looks like you are trying to use client.c/server.c from here: http://www.linuxhowtos.org/C_C++/socket.htm
If I use those files, it works fine. So start over from them and apply your changes more carefully.