Socket Programming in C
- From: "josh" <slaughter.josh@xxxxxxxxx>
- Date: 2 Mar 2007 17:07:38 -0800
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;
}
.
- Follow-Ups:
- Re: Socket Programming in C
- From: Beej Jorgensen
- Re: Socket Programming in C
- Prev by Date: Re: freebsd send syscall and cluster size
- Next by Date: Re: regarding eigen.h
- Previous by thread: Re: cpuinfo on FreeBSD
- Next by thread: Re: Socket Programming in C
- Index(es):
Relevant Pages
|