LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2006, 09:53 AM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
Making a server/client connection


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");

while (1) { /* loop forever */

client_len = sizeof(cli_addr);
/* Accept the client connection */
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client_len);
if (newsockfd < 0)
fprintf(stderr, "server: accept error");

/* Read string from the socket */

read(newsockfd, userstring, BUFFSIZE);

/*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.
 
Old 02-07-2006, 10:17 AM   #2
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
In your client code these two lines are your problem:

Code:
char userstring; /* String from the user */
char revstring; /* String reversed from the server */
You've declared char's but they need to be of type char arrays. Replace those with the following and it works:
Code:
char userstring[BUFFSIZE]; /* String from the user */
char revstring[BUFFSIZE]; /* String reversed from the server */

Last edited by oulevon; 02-07-2006 at 10:19 AM.
 
Old 02-07-2006, 10:30 AM   #3
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Now I get this error:


68: error: incompatible types in assignment

is it because in my reverse function it is declared as a char pointer?

Thanks for your help!
 
Old 02-07-2006, 10:31 AM   #4
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
this is line 68:

revstring = reverse(userstring);
 
Old 02-07-2006, 10:51 AM   #5
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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?
 
Old 02-07-2006, 11:05 AM   #6
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Your right, my brain is fried. THanks for your help it works beautiful!
 
Old 02-07-2006, 11:21 AM   #7
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LDAP SERVER Client Connection on SUSE 9.2 - connection error jcarton Linux - Networking 3 03-19-2005 12:40 PM
LDAP SERVER - CLIENT CONNECTION on suse 9.2 - connection error nicolasdiogo Linux - Networking 4 03-01-2005 01:43 PM
LDAP SERVER - CLIENT CONNECTION on suse 9.2 - connection error nicolasdiogo SUSE / openSUSE 0 03-01-2005 05:43 AM
Showing CLIENT<-server->INTERNET connection from server. druuna Linux - Networking 2 05-03-2004 01:33 PM
making a secure client/server connection with command prompt williamp0044 *BSD 4 03-06-2004 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 03:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration