I'm trying to make a client program for a project in my class. The project has the client send the server an option, and the server send the client the required information. The trouble is, my client can't send anything right. The client is supposed to send a number to the server. I'm using a test server to simply display what it receives from the client to the screen. Any number other than a 1 doesn't make it to the server (although the server opens and closes the connection with the client just fine), and a 1 tells the server to display a smiley face. It's freaky because I'm working in a text only UNIX environment. I know it's probably something really easy, but the answer is eluding me. I suspect it has something to do with the byte order, though. Could you guys help me?
Code for the client
Code:
// Preprocessor Directives
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include "books.h"
#define SERVER_PORT 6005
// Function Declarations
int menu();
// Main
int main( int argc, char * argv[] )
{
// Variable Declarations
int sockfd; // Variable to store the return value of socket()
int i; // Loop counter variable
int choice; // Pointer to variable to store menu choice
int sendok; // Variable to store return value of send()
unsigned short int hello;
struct sockaddr_in localAddr, servAddr; // Variables to store local and server addresses
struct hostent *h; // Variable to store IP address of host
// Command Line Error Checking
if ( argc < 2 )
{
printf ( "usage: %s <server>\n", argv[0] );
exit ( 1 );
}
// Resolving host name
h = gethostbyname( argv[1] );
// Host Name Error Checking
if ( h == NULL )
{
printf( "%s: unknown host '%s'\n", argv[0], argv[1] );
exit( 1 );
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
// Opening TCP Connection
// Creating Socket
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
if ( sockfd == -1 )
{
perror( "Error: Cannot open socket." );
exit( 1 );
}
// Bind Socket to Any Port Number
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
if ( (bind( sockfd, (struct sockaddr *) &localAddr, sizeof( localAddr ))) < 0 )
{
printf( "%s: Cannot bind port TCP %u \n", argv[0], SERVER_PORT );
perror( "error ");
exit( 1 );
}
// Connect to Server
if ( (connect( sockfd, (struct sockaddr *) &servAddr, sizeof( servAddr ))))
{
perror( "Error: Cannot connect" );
exit( 1 );
}
// Program Menu
choice = menu();
if( ( send(sockfd, &choice, sizeof( int ), 0) ) < 0 )
{
perror( "Cannot send data " );
close( sockfd );
exit( 1 );
}
// Sever the TCP Connection
close( sockfd );
return 0;
}
// Function Definitions
// Program Menu Function
int menu()
{
int choice;
printf( "Please type the number of the choice you want.\n\n" );
printf( "1 - List inventory contents and total price.\n" );
printf( "2 - Search books by title.\n" );
printf( "3 - Check out a book.\n" );
printf( "4 - Search books by author.\n" );
printf( "5 - Quit.\n\n" );
printf( "Your choice: " );
scanf( "%d", &choice );
return choice;
}
Code for a server to test my client
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */
#define SUCCESS 0
#define ERROR 1
#define END_LINE 0x0
#define SERVER_PORT 6005
#define MAX_MSG 100
/* function readline */
int read_line();
int main (int argc, char *argv[]) {
int sd, newSd, cliLen;
struct sockaddr_in cliAddr, servAddr;
char line[MAX_MSG];
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
return ERROR;
}
/* bind server port */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SERVER_PORT);
if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
perror("cannot bind port ");
return ERROR;
}
listen(sd,5);
while(1) {
printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);
cliLen = sizeof(cliAddr);
newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
if(newSd<0) {
perror("cannot accept connection ");
return ERROR;
}
/* init line */
memset(line,0x0,MAX_MSG);
/* receive segments */
while(read_line(newSd,line)!=ERROR) {
printf("%s: received from %s:TCP%d : %s\n", argv[0],
inet_ntoa(cliAddr.sin_addr),
ntohs(cliAddr.sin_port), line);
/* init line */
memset(line,0x0,MAX_MSG);
} /* while(read_line) */
} /* while (1) */
}
/* rcv_line is my function readline(). Data is read from the socket when */
/* needed, but not byte after bytes. All the received data is read. */
/* This means only one call to recv(), instead of one call for */
/* each received byte. */
/* You can set END_CHAR to whatever means endofline for you. (0x0A is \n)*/
/* read_lin returns the number of bytes returned in line_to_return */
int read_line(int newSd, char *line_to_return) {
static int rcv_ptr=0;
static char rcv_msg[MAX_MSG];
static int n;
int offset;
offset=0;
while(1) {
if(rcv_ptr==0) {
/* read data from socket */
memset(rcv_msg,0x0,MAX_MSG); /* init buffer */
n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */
if (n<0) {
perror(" cannot receive data ");
return ERROR;
} else if (n==0) {
printf(" connection closed by client\n");
close(newSd);
return ERROR;
}
}
/* if new data read on socket */
/* OR */
/* if another line is still in buffer */
/* copy line into 'line_to_return' */
while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) {
memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1);
offset++;
rcv_ptr++;
}
/* end of line + end of buffer => return line */
if(rcv_ptr==n-1) {
/* set last byte to END_LINE */
*(line_to_return+offset)=END_LINE;
rcv_ptr=0;
return ++offset;
}
/* end of line but still some data in buffer => return line */
if(rcv_ptr <n-1) {
/* set last byte to END_LINE */
*(line_to_return+offset)=END_LINE;
rcv_ptr++;
return ++offset;
}
/* end of buffer but line is not ended => */
/* wait for more data to arrive on socket */
if(rcv_ptr == n) {
rcv_ptr = 0;
}
} /* while */
}