Issuu on Google+

MTIT Socket Programming Assignment (Simple Network Chat System) Student ID

Student Name

IT09113374

K.K.D De Silva

IT09150522

W.A.D.M.Silva

IT09135154

P.H.R.A Darshana

IT09150904

B.M.N.A Basnayake


Compilation commands : server : gcc server.c ­lpthread ­oserver client : gcc client.c ­lpthread ­oclient Execution Commands : server : ./server client : ./client Screenshots :


Screenshots of runtime programs.

Server


Sourcecodes Server: 1: /* server.c 2: Student ID Student Name 3: IT09113374 K.K.D De Silva 4: IT09150522 W.A.D.M.Silva 5: IT09135154 P.H.R.A Darshana 6: IT09150904 B.M.N.A Basnayake 7: */ 8: 9: #include <sys/types.h> 10: #include <sys/socket.h> 11: #include <netinet/in.h> 12: #include <arpa/inet.h> 13: #include <stdio.h> 14: #include <stdlib.h> 15: #include <unistd.h> 16: #include <errno.h> 17: #include <string.h> 18: 19: #include <pthread.h> 20:

//header file used to archieve Threading in C

21: void *handleclient_thread(void *arg); 22: 23: typedef struct{ 24: char username[10]; 25: } user_typ; 26: 27: pthread_mutex_t users_mutex;

//used to achieve mutant locks

28: user_typ users[50]; 29: 30: int main() 31: { 32:

pthread_t client_thread;

33:

int sock, connected, bytes_recieved , true = 1;

34: UserID 35:

char send_data [1024] , recv_data[1024];

36:

struct sockaddr_in server_addr,client_addr;

37: 38:

int sin_size;

39:

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

40:

perror("Socket");

41:

exit(1);

42: 43:

}

//first 4 characters of recv_data is used to mention


if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {

44: 45:

perror("Setsockopt");

46:

exit(1);

47: 48:

}

49:

server_addr.sin_family = AF_INET;

50:

server_addr.sin_port = htons(5000);

51:

server_addr.sin_addr.s_addr = INADDR_ANY;

52: 53:

bzero(&(server_addr.sin_zero),8);

54:

if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))

== -1) {

55: 56:

perror("Unable to bind");

57:

exit(1);

58: 59:

}

60:

if (listen(sock, 5) == -1) {

61:

perror("Listen");

62:

exit(1);

}

63: 64: 65:

printf("\nTCPServer Waiting for client on port 5000\n"); fflush(stdout);

66: 67: 68: 69:

int * connfd;

70:

for(;;)

71:

//an infinite loop

{

72:

connfd=malloc(sizeof(int));

73:

printf("waiting\n");

74:

*connfd=accept(sock,(struct sockaddr *)&client_addr,&sin_size); pthread_create(&client_thread,NULL,handleclient_thread, connfd);

75: 76: 77:

}

78:

pthread_join(client_thread, NULL /* void ** return value could go here */);

79:

close(sock);

80:

return 0;

81: } 82: 83: 84: void *handleclient_thread(void *arg)//function pointer 85: { 86: //char *userID; 87: //userID=(char*)arg; 88:

int socket_handlr,bytes_recieved;


89:

socket_handlr= *((int *)arg);

90: 91:

printf("connected handlr : %d\n",socket_handlr);

92:

char send_data[1024];

93:

int n=sprintf(send_data,"Client connected %d\n",socket_handlr);

94: 95:

printf("%s",send_data);

96: 97: 98:

char recv_data[1024];

99: 100:

//first 4 characters of recv_data is used to mention UserID

while (1) //an infinite loop

{

101: 102:

bytes_recieved = recv(socket_handlr,recv_data,1024,0);

103: 104:

recv_data[bytes_recieved] = '\0';

105:

if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)

106:

{

107:

close(socket_handlr);

108:

break;

109: 110: 111:

} else

112: 113: 114:

printf("\n RECIEVED DATA = %s " , recv_data);

115:

printf("\n SEND (q or Q to quit) : ");

116: 117:

gets(send_data);

118:

if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)

119:

{

120:

send(socket_handlr, send_data,strlen(send_data), 0);

121:

close(socket_handlr);

122:

break;

}

123: 124: 125:

else send(socket_handlr, send_data,strlen(send_data), 0);

126: 127:

fflush(stdout);

128: 129: 130: }

}


Client : 1: /* client.c 2: Student ID Student Name 3: IT09113374 K.K.D De Silva 4: IT09150522 W.A.D.M.Silva 5: IT09135154 P.H.R.A Darshana 6: IT09150904 B.M.N.A Basnayake 7: */ 8: 9: #include <sys/socket.h> 10: #include <sys/types.h> 11: #include <netinet/in.h> 12: #include <netdb.h> 13: #include <stdio.h> 14: #include <string.h> 15: #include <stdlib.h> 16: #include <unistd.h> 17: #include <errno.h> 18: 19: 20: 21: int main() 22: 23: { 24:

int sock, bytes_recieved;

25:

char send_data[1024],recv_data[1024];

26:

struct hostent *host;

27: 28:

struct sockaddr_in server_addr;

29: 30:

host = gethostbyname("127.0.0.1");

31:

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

32:

perror("Socket");

33:

exit(1);

34: 35:

}

36:

server_addr.sin_family = AF_INET;

37:

server_addr.sin_port = htons(5000);

38:

server_addr.sin_addr = *((struct in_addr *)host->h_addr);

39: 40:

bzero(&(server_addr.sin_zero),8);

41:

if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)

42: 43:

{

44:

perror("Connect");

45:

exit(1);


46: 47:

}

48:

while(1)

49:

{

50:

printf("\nSEND (q or Q to quit) : ");

51: 52:

gets(send_data);

53:

if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)

54: 55: 56:

send(sock,send_data,strlen(send_data), 0); else

57:

{

58:

send(sock,send_data,strlen(send_data), 0);

59:

close(sock);

60:

break;

61: 62: 63:

}

64:

bytes_recieved=recv(sock,recv_data,1024,0);

65: 66:

recv_data[bytes_recieved] = '\0';

67:

if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)

68:

{

69:

close(sock);

70:

break;

71: 72: 73:

}

74: 75:

printf("\nRecieved data = %s " , recv_data);

76: 77:

else

}

78: return 0; 79: }


Sample Doc