50% found this document useful (2 votes)
701 views

Report of Socket Programming Assignment

Socket programming using C programming language. This assignment is about creating client and server that can connect and play Guessing Game. Server generates random number and client has to guess the number. Server will display the number of attempt and high score. Feel free to review.

Uploaded by

ans
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
701 views

Report of Socket Programming Assignment

Socket programming using C programming language. This assignment is about creating client and server that can connect and play Guessing Game. Server generates random number and client has to guess the number. Server will display the number of attempt and high score. Feel free to review.

Uploaded by

ans
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CST 234 - Network Programming

Semester 2, session 2013/2014



Assignment-Socket Programming


Lecturer's name :
Mr. G.C. Sodhy
Group Members :
Nur Izzati Zafirah Binti Zulkipli 115100
Aizattul Anis Binti Sobri 114998

Date of Submission:
25th March 2014



How the application (both server and client ) works:
This report is mainly based on the server, which is ./gg-tcpservs and client application, ./gg-
tcpcli with the local host address 127.0.0.1. This application could also be used by other PC's
connecting to that particular server. The application use various function to make client able
to connect to server and play Guessing Game such as listen function, bind function, signal
function, fork function, select function, accept function and str_cli function.
listenfd = Socket(AF_INET, SOCK_STREAM, 0);
This function is called only by the TCP server in order for the server to listen for the
incoming clients, which needs to connect to the server. The bind function assigns a local
protocol address to a socket. When client (in this application, user) try to make handshake
(connect) with server, the server will listen and start to make connection with the client. After
client connected to server, client can start playing the game.
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
After a new connection from client is established by the server and the server accept the
connection, there will be a print message popping out on the server side. It will show there is
a new client connected to the server ( IP address of the client as well as port number will be
displayed on server side. On client side also the game instruction will be displayed.
Write(sockfd, buf, sizeof(buf));
This function is used to display total number of client that is currently connected with server.
There is a counter in for loop, in order for it to display the number of clients which are
getting connected to the server. Each time a new client connected to the server, the counter++
will keep track of it by increasing the counter.
When a certain client which is connected to the server, when that client terminates,
automatically the server terminates also because the menu or the options are provided by that
particular server. There will be a message saying the "server terminated prematurely" to
indicate that the server has terminated and there will be no communication among the server
and client, in other words.


Signal(SIGCHLD, sig_chld);
Fork system call is used to create child process. The SIGCHLD signal is sent to the parent of
a child process when it exits, interrupted, or resumes after being interrupted.


Source Code (Server)
#include "unp.h"
#include <time.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <stdio.h>

int main(int argc, char **argv)
{
int listenfd, connfd;
pid_t childpid;
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
char buff[MAXLINE];
void sig_chld(int);

listenfd = Socket(AF_INET, SOCK_STREAM, 0); //return small integer descriptor and
identify socket in all future function call

/* Assign unique new address */
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);

/* Bind our well-known port so the client can connect to us. */
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

Listen(listenfd, LISTENQ); //specify size of request queue

Signal(SIGCHLD, sig_chld); //prevent child from being zombies, call wait

for ( ; ; ) {
clilen = sizeof(cliaddr);

if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
if (errno == EINTR) {
continue; /* back to for() */
}
else
err_sys("accept error");
}

if ( (childpid = Fork()) == 0) { // child process
Close(listenfd); //close listening socket
printf("child process\n");
printf("connection from %s, port %d\n",Inet_ntop(AF_INET, &cliaddr.sin_addr, buff,
sizeof(buff)),ntohs(cliaddr.sin_port)); //print client ip and port number
str_echo(connfd); // process the request
exit(0);
}
Close(connfd); // parent closes connected socket
}

}

struct args {
int arg1 ;
};


void str_echo(int sockfd)
{

ssize_t n;
char buf[MAXLINE];
struct args args;

int s1 , s2 ;
int score=0 ;
int hscore=0;

again:

srand(time(NULL));
int a = rand()% 100 + 1; // generate random number 1 to 99
int m=1;
int l=a-5;

int h=a+5;

printf("\n\nserver number is %d\n\n ",a); //print number to be guessed on server


for ( ; ; ) {


if ( (n = Readn(sockfd, &args, sizeof(args))) == 0)
return; /* connection closed by other end */


printf("guess number made by client %d\n",args.arg1);


/* Server response for guessing and comparison */
if (args.arg1 == a)
{
score=100-m;

if (m<10) {

snprintf(buf, sizeof(buf), " woww youre fast \nyou guess the correct number at attempt
number :%d \n youre score is :%d \n\nokey lets guess another number",m,score);

printf("number of attempt by client: %d\nclient score is :%d\n ",m,score);

Write(sockfd, buf, sizeof(buf));

}
else if (m<15 && m>10){

snprintf(buf, sizeof(buf), "ok you made it \nyou guess the correct number at attempt number
:%d \n youre score is :%d \n\nokey lets guess another number",m,score);

printf("number of attempt by client: %d\nclient score is :%d\n ",m,score);


Write(sockfd, buf, sizeof(buf));

}

else if (m>10){

snprintf(buf, sizeof(buf), "at last you made it \n you gues the correct number at attempt
number\n :%d\nyoure score is :%d \n\nokey lets guess another number",m,score);

printf("number of attempt by client: %d\nclient score is :%d\n ",m,score);


Write(sockfd, buf, sizeof(buf));

}

if(m<s1 && s1!=0)
{ s2=m;
hscore=100-s2;

}

else
{
hscore=100-s1;

}

s1=m;

goto again;
}



/*Display number of attempt and high score when client want to exit*/
else if ( args.arg1==999)
{
snprintf(buf, sizeof(buf), "\nthe number is :%d\nthank you for playing\nyour high score is
%d",a,hscore);


Write(sockfd, buf, sizeof(buf));
break;

}

else if ( args.arg1==888)
{
snprintf(buf, sizeof(buf), "\nthanks for playing, your high score is : %d\nbyee byee...thanks
for playing",hscore);

Write(sockfd, buf, sizeof(buf));
break;

}

else if ( args.arg1<h && args.arg1>a){

snprintf(buf, sizeof(buf), "little bit high \n");
printf("little bit high \n");

if (m>5) {
snprintf(buf, sizeof(buf), "\nlittle bit high \nif you want to give up enter 999, we will help to
show you the number \n");

}
}
else if ( args.arg1> a){

snprintf(buf, sizeof(buf), "too high\n");
printf("too high \n");

if (m>5) {
snprintf(buf, sizeof(buf), "too high\nif you want to give up enter 999, we will help to show
you the number \n");

}

}
else if ( args.arg1<a && args.arg1>l){

snprintf(buf, sizeof(buf), "little bit low \n");
printf("little bit low \n");
if (m>5) {
snprintf(buf, sizeof(buf), "little bit low\nif you want to give up enter 999, we will help to
show you the number \n");

}

}

else if ( args.arg1<a)
{
snprintf(buf, sizeof(buf), "too low\n");
printf("too low \n");
if (m>5) {
snprintf(buf, sizeof(buf), "too low\nif you want to give up enter 999, we will help to show
you the number \n");

}
}

else
snprintf(buf, sizeof(buf), "wrong number\n");


Write(sockfd, buf, sizeof(buf));
m++;
}
}

/*Client will be terminated*/
void sig_chld(int signo)
{
pid_t pid;
int stat;

while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0)
printf("child %d terminated\n\n", pid);

return;
}
/* Below is the coding to make server able to handle the situation of server down or unreachable by
client */
void str_cli(FILE *fp, int sockfd)
{
int maxfdp1;
fd_set rset;
char sendline[MAXLINE], recvline[MAXLINE];

FD_ZERO(&rset);
for ( ; ; ) {
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdp1 = max(fileno(fp), sockfd) + 1;

Select(maxfdp1, &rset, NULL, NULL, NULL );


if (FD_ISSET(sockfd, &rset)) { /* socket is readable */
if (Readline(sockfd, recvline, MAXLINE) == 0)
err_quit("str_cli: server terminated prematurely"); //server is terminated if
client cannot connect
Fputs(recvline, stdout);
}

if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
if (Fgets(sendline, MAXLINE, fp) == NULL)

return; /* all done */
Writen(sockfd, sendline, strlen(sendline));
}
}
}

Source Code (Client)
#include "unp.h"

int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
void str_cli(FILE *fp, int sockfd);

if (argc != 2)
err_quit("usage: tcpcli <IPaddress>");

sockfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

printf("\n\n ~~~~~~~welcome to guessing number game ~~~~~~~~~~\n\n");

printf(" I'm thinking in a number between 1 end 100...\n: ");

printf(" \n guess my number \n ");

printf(" \n if you want to exit the game enter '888' \n ");

printf(" \n~~~~~~~~~~~~~~~~~~~ good luck ~~~~~~~~~~~~~~~~~~~\n ");

str_cli(stdin, sockfd); /* do it all */

exit(0);
}
struct args {
int arg1 ;

};

void str_cli(FILE *fp, int sockfd)
{
char sendline[MAXLINE];
char recvline[MAXLINE];
struct args args;


while (Fgets(sendline, MAXLINE, fp) != NULL) {

if (sscanf(sendline, "%d", &args.arg1 ) != 1)
{
printf("invalid input: %s\n", sendline);
continue;
}

Write(sockfd, &args, sizeof(args));

if (Readn(sockfd, recvline, MAXLINE) == 0)
err_quit("str_cli: server terminated prematurely");


Fputs(recvline, stdout);
printf("\n");

}
}


How server and client works

You might also like