0% found this document useful (0 votes)
139 views

Raport: Ministerul Educației Al Republicii Moldova Universitatea Tehnică A Moldovei

This document appears to be a lab report for a networking and systems programming course. It includes source code for a client and server program written in C. The client program connects to the server, sends a text string, and closes the socket. The server program creates a listening socket bound to a port, spawns a new thread for each incoming connection to process requests, and prints out any received text. The report also shows example runs of the client program sending messages to the server.

Uploaded by

Iulian Decuseara
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Raport: Ministerul Educației Al Republicii Moldova Universitatea Tehnică A Moldovei

This document appears to be a lab report for a networking and systems programming course. It includes source code for a client and server program written in C. The client program connects to the server, sends a text string, and closes the socket. The server program creates a listening socket bound to a port, spawns a new thread for each incoming connection to process requests, and prints out any received text. The report also shows example runs of the client program sending messages to the server.

Uploaded by

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

Ministerul Educației al Republicii Moldova

Universitatea Tehnică a Moldovei


Departamentul Informatică și Ingineria Sistemelor

RAPORT
Lucrarea de laborator Nr.8 la
Programare de sistem și de reţea

A efectuat:

St. gr. C-171 Davîdic V.

A verificat:

dr., conf.univ. O.Godonoga

Chișinău 2020
Cod Client:
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>

int main(int argc, char ** argv)


{ int port; int sock = -1;
struct sockaddr_in address;
struct hostent * host;
int len;

/* checking commandline parameter */ if


(argc != 4)
{
printf("usage: %s hostname port text\n", argv[0]);
return -1;
}

/* obtain port number */


if (sscanf(argv[2], "%d", &port) <= 0)
{
fprintf(stderr, "%s: error: wrong parameter: port\n", argv[0]);
return -2;
}

/* create socket */
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if
(sock <= 0)
{
fprintf(stderr, "%s: error: cannot create socket\n", argv[0]);
return -3;
}

/* connect to server */ address.sin_family


= AF_INET; address.sin_port = htons(port);
host = gethostbyname(argv[1]);
if (!host)
{
fprintf(stderr, "%s: error: unknown host %s\n", argv[0], argv[1]);
return -4;
}
memcpy(&address.sin_addr, host->h_addr_list[0], host->h_length);

if (connect(sock, (struct sockaddr *)&address, sizeof(address)))


{
fprintf(stderr, "%s: error: cannot connect to host %s\n", argv[0], argv[1]);
return -5;
}

/* send text to server */ len =


strlen(argv[3]); write(sock,
&len, sizeof(int)); write(sock,
argv[3], len);

/* close socket */
close(sock);

return 0;
}

Cod Server:
#include <stdio.h>
#include <pthread.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <stdlib.h>

typedef struct
{ int sock;
struct sockaddr address;
int addr_len;
} connection_t;

void * function(void *);

void * process(void * ptr)


{
char * buffer; int len;
connection_t * conn; long
addr = 0;

if (!ptr) pthread_exit(0); conn


= (connection_t *)ptr;

/* read length of message */


read(conn->sock, &len, sizeof(int)); if
(len > 0)
{
addr = (long)((struct sockaddr_in *)&conn->address)->sin_addr.s_addr;
buffer = (char *)malloc((len+1)*sizeof(char));
buffer[len] = 0;

/* read message */ read(conn-


>sock, buffer, len); /*
print message */
printf("%d.%d.%d.%d: %s\n",
(addr ) & 0xff,
(addr >> 8) & 0xff,
(addr >> 16) & 0xff,
(addr >> 24) & 0xff,
buffer); free(buffer);
}
/* close socket and clean up */
close(conn->sock); free(conn);
pthread_exit(0);
}

int main(int argc, char ** argv)


{ int sock = -1; struct
sockaddr_in address; int port;
connection_t * connection; pthread_t
thread;
/* check for command line arguments */ if
(argc != 2)
{
fprintf(stderr, "usage: %s port\n", argv[0]);
return -1;
}

/* obtain port number */ if


(sscanf(argv[1], "%d", &port) <= 0)
{
fprintf(stderr, "%s: error: wrong parameter: port\n", argv[0]);
return -2;
}

/* create socket */
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if
(sock <= 0)
{
fprintf(stderr, "%s: error: cannot create socket\n", argv[0]);
return -3;
}

/* bind socket to port */ address.sin_family


= AF_INET; address.sin_addr.s_addr =
INADDR_ANY; address.sin_port =
htons(port);
if (bind(sock, (struct sockaddr *)&address, sizeof(struct sockaddr_in)) < 0)
{
fprintf(stderr, "%s: error: cannot bind socket to port %d\n", argv[0], port);
return -4;
}
/* listen on port */
if (listen(sock, 5) < 0)
{
fprintf(stderr, "%s: error: cannot listen on port\n", argv[0]);
return -5;
}
printf("%s: ready and listening\n", argv[0]); while
(1)
{
/* accept incoming connections */
connection = (connection_t *)malloc(sizeof(connection_t));
connection->sock = accept(sock, &connection->address, &connection->addr_len); if
(connection->sock <= 0)
{
free(connection);
}
else {
/* start a new thread but do not wait for it */
pthread_create(&thread, 0, process, (void *)connection);
pthread_detach(thread);
}
}
return 0; }

davidicvlad@debian
davidicvlad@debian : -/ АВВ $ ./client localhost 4008 textl
davidicvlad @debian : -/ А 8$ ./client localhost 400Q text2
:-/LAB 8$ ./client loca1host 4000 textз

davidicvlad@debian davidicvlad@debian
: - /LAB 8$ ./client :- / АВ8 ./client localhost 4006 t2
localhost 4000 tl

davidicvlad@debian
davidicvlad@debian :-/L В8$ ./client localhost 4000 al
:-/ АВ8$ ./client localhost 4000 а2

You might also like