Socket Programming in C



Hi, I'm new to socket programming and what I am trying to do is create
a client and server program that simulates rolling a die. Hopefully I
can explain this well enough. The client writes an integer to the
socket and the server will take the input from the socket as the
number of die to roll. The server will generate a random number for
each die roll and then write the results to the socket and display the
result as an array of numbers. Here is the code that I have written
so far but I can't see to get it to work. Thanks in advance for your
help.

//---------------------------------------------------------- Server.c
---------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define RCVBUFSIZE 256

void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int servSock, clntSock, port, clntLen;
char RcvBuffer[RCVBUFSIZE];
int rolls, i, diceroll, die1, die2, die3, die4, die5, die6;
int results[5];

// Local address
struct sockaddr_in ServAddr, ClntAddr;

if (argc < 2)
{
fprintf(stderr,"ERROR, port not provided\n");
exit(1);
}

// Create socket for incoming connections
if ((servSock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
error("socket() failed");

// Construct local address structure
bzero((char *) &ServAddr, sizeof(ServAddr)); //Initialize structure
to 0
port = atoi(argv[1]);
ServAddr.sin_family = AF_INET; // Internet address
family
ServAddr.sin_addr.s_addr = INADDR_ANY; // Any incoming interface
ServAddr.sin_port = htons(port);

// Bind to the local address
if(bind(servSock, (struct sockaddr *) &ServAddr,
sizeof(ServAddr)) < 0)
error("bind failed");

// Mark the socket so it will listen for incoming connections
listen(servSock,5);
for (;;)
{
clntLen = sizeof(ClntAddr);

// Wait for Client to connect
clntSock = accept(servSock, (struct sockaddr *) &ClntAddr,
&clntLen);

if (clntSock < 0)
error("accept() failed");
// Initialize receive buffer to 0
bzero(RcvBuffer,RCVBUFSIZE);

// Receive input from client
int val;
val = read(clntSock, RcvBuffer, RCVBUFSIZE -1);

die1 = RcvBuffer;

die1 = die1*2;
if (val < 0)
error("Receive failed \n");
bzero(RcvBuffer,RCVBUFSIZE);

for (i=0;i<die1;i++)
{
diceroll = 1 + (rand() % 6);
RcvBuffer[i] = (char)diceroll;
RcvBuffer[i++] = " ";
i++;
}

val = write(clntSock,RcvBuffer,255);
}
return 0;
}




//----------------------------------------------------------- Client.c
-----------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFFERSIZE 256

void error(char *msg)
{
perror(msg);
exit(1);
}

int main(int argc, char *argv[])
{
int s, port, client, client_val;
char buffer[BUFFERSIZE];

//socket address information
struct sockaddr_in addr;

//info about the host that client will connect to
struct hostent* host;


// Check for proper number of arguments
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}

port = atoi(argv[2]);

//Get socket descriptor file
s = socket(AF_INET, SOCK_STREAM, 0);

//Get host information
host = gethostbyname(argv[1]);

// Zero out socket structure
bzero((char *) &addr, sizeof(addr));

// Construct the server address structure
addr.sin_family = AF_INET;

memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length);

addr.sin_port = htons(port);

// Establish the connection to the Server
if(connect(s, (struct sockaddr*)&addr, sizeof(addr)) <0)
error("couldn't connect");

// Send the value of the dice to the server
printf("Enter the number of dice to roll: ");

// Zero out buffer structure
bzero(buffer,BUFFERSIZE);

fgets(buffer, BUFFERSIZE - 1, stdin);

int val;

// Send the value to the Server
if(val = write(s,buffer,strlen(buffer)) < 0) error("failed to
write to socket");
//if(write(s, &val, sizeof(val)) < 0) error("failed to write to
socket");

// Zero out buffer structure
bzero(buffer,BUFFERSIZE);

// Wait for the Server to return a value
if(val = read(s, buffer, BUFFERSIZE - 1) < 0) error("couldn't
receive value from server");

printf("Value from server: %d\n", buffer[0]);

// Close the socket and exit
close(s);

return 0;
}

.



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... Client client = new Client; ...
    (microsoft.public.vc.language)
  • Re: TCP server stop receiving new connections
    ... reset the event mask of your listening socket each time you ... I have a strange problem in my class library used by all our client ... server applications. ... incomming connections, but keeps current connections. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)