Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
From my client I am trying to send a string, the server needs to reverse the characters and send it back to the client.
SERVER CODE::
/******************************************************************
This is a simple example of a server using TCP
*****************************************************************/
/* Library Section */
#include <stdio.h> /* Standard Input and Output */
#include <unistd.h> /* Unix Standard Functions */
#include <string.h> /* Used for bzero */
#include <math.h> /* Math Library used for log10 */
#include <sys/types.h> /* Used for System Data Type Defitions */
#include <sys/socket.h> /* Used for sockets */
#include <netinet/in.h> /* sockaddr_in definition */
#include <arpa/inet.h> /* Internet Sockets */
char* reverse(char s[]);
/* Definitions */
#define SERV_TCP_PORT 10617 /* Port Number */
#define SERV_HOST_ADDR "198.102.147.137" /* IP address for Gimli */
#define BUFFSIZE 500 /* Buffer Size */
int main() {
int sockfd; /* Initial socket descriptor */
int newsockfd; /* Client/Server socket descriptor */
socklen_t client_len; /* Client Socket File Length */
struct sockaddr_in serv_addr; /* Server Address Information */
struct sockaddr_in cli_addr; /* Client Address Information */
char msgbuff[BUFFSIZE]; /* Message Buffer */
char userstring[BUFFSIZE]; /* string from the user */
char* revstring; /* string reversed from the server */
/* Open a TCP socket (an Internet stream socket). */
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)/* creating internet socket */
fprintf(stderr, "server: can't open stream socket\n");
/* Bind our local address so that the client can send to us. */
/* First, clear out the structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(SERV_TCP_PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
fprintf(stderr, "server: can't bind local address\n");
/* Now, listen for incomming connections. */
listen(sockfd, 5);
printf("The server is now listening for connections\n");
/*reverse the string */
revstring = reverse(userstring);
/* Write string to the socket */
write(newsockfd, revstring, strlen(revstring));
close(newsockfd); /* We are finished with the socket */
bzero(msgbuff, BUFFSIZE);
} /* END WHILE */
return (0); /* This will never happen since in infinite loop */
}
char* reverse(char s[])
{
int c, i, j;
for (i=0, j=strlen(s)-1; i < j;i++, j--)
{
c = s[i];
s[i] = s [j];
s[j] = c;
}
return s;
}
CLIENT CODE::
/* Library Section */
#include <stdio.h> /* Standard Input and Output */
#include <unistd.h> /* Unix Standard Functions */
#include <math.h> /* Math Library for log function */
#include <string.h> /* Used for bzero */
#include <sys/types.h> /* Used for System Data Type Defitions */
#include <sys/socket.h> /* Used for sockets */
#include <netinet/in.h> /* sockaddr_in definition */
#include <arpa/inet.h> /* Internet Sockets */
/* Definitions */
#define SERV_TCP_PORT 10617 /* Port Number */
#define SERV_HOST_ADDR "198.102.147.137" /* IP address for Gimli */
#define BUFFSIZE 500 /* Buffer Size */
int main() {
int sockfd; /* Initial socket descriptor */
struct sockaddr_in serv_addr; /* Server Address Information */
/*char climsg[BUFFSIZE];*/ /* Client Message */
char userstring; /* String from the user */
char revstring; /* String reversed from the server */
/* Fill in the structure of "serv_addr" with the address of the
* server that we want to connect with.
*/
/* First, zero out the structure. */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
serv_addr.sin_port = htons(SERV_TCP_PORT);
/* Open a TCP socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
fprintf(stderr, "Client: can't open stream socket");
/* Connect to the server */
if (connect(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
fprintf(stderr, "Client: can't connect to server");
/* Get string from the user */
printf("Please enter a string to be reversed by the server");
scanf("%s", &userstring);
/* Write string to the socket */
write(sockfd, userstring, strlen(userstring)); /* or size +2 */
/* Read string from the socket */
read(sockfd, revstring, BUFFSIZE);
printf("The string %s reversed is %s\n", userstring, revstring);
close(sockfd);
return (0); /* A zero indicates a normal closure */
}
I can get the server to listen for connections. When I send the message to the server I get a segmentation fault. Some one suggested I have a problem with my reverse function but I can not figure it out.
I don't get any errors on my machine. It seems the error your reporting is from your server.c file, since this is the only place your reverse function is called. You shouldn't get an error in that file because there have been no changes to that file and it's independent to the client.c file.
Did you make the changes in the client.c file or the server.c file?
No problem. I make mistakes like that all the time too. If you have more questions like this, you might get a better response in the programming forum.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.