1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
// connect with the specified server
int connect_to_server_ipv4(const char * adr, const char * port,client_ipv4 * tab_client)
{
int sock= -1;
// create socket
if( ( sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) ) == -1)
{
perror("socket");
return -1;
}
// get addr from command line and convert it
if( inet_pton(AF_INET,adr, &( (tab_client->server).sin_addr) )!= 1 )
{
perror("inet_pton ipv4");
close(sock);
return -1;
}
/* Connection datas */
memset( &(tab_client->server ), '\0', sizeof(struct sockaddr_in));
tab_client->server.sin_family = AF_INET;
tab_client->server.sin_port = htons(atoi(port));
tab_client->recsize = sizeof(struct sockaddr_in);
// bind addr structure with socket
memset( &(tab_client->client ), '\0', sizeof(struct sockaddr_in));
tab_client->client.sin_family = AF_INET;
tab_client->client.sin_port = htons(0);
tab_client->client.sin_addr.s_addr = htonl(INADDR_ANY);
tab_client->recsize = sizeof(struct sockaddr_in);
if(bind(sock,(struct sockaddr *)&(tab_client->client), tab_client->recsize ) == -1)
{
perror("Error : Bind socket");
close(sock);
return -1;
}
printf("Connection à %s on port %d established in Ipv4\n",
inet_ntoa((tab_client->server).sin_addr),
htons((tab_client->server).sin_port));
return sock;
}
//communicate with the server, send message from keyboard and print received message
int communicate_with_server_ipv4(client_ipv4 * tab_client, int sock)
{
int size_s = 0;
int size_r = 0;
char w_buffer[BUF_SIZE];
char r_buffer[BUF_SIZE];
memset(w_buffer, '\0', sizeof(char) * BUF_SIZE);
memset(r_buffer, '\0', sizeof(char) * BUF_SIZE);
fd_set readfd;
fd_set readfd2;
while (1)
{
FD_ZERO(&readfd);
FD_SET(0, &readfd);
FD_SET(sock,&readfd);
if( select (sock+1,&readfd, NULL,NULL,NULL) < 0 )
{
perror("select");
return -1;
}
readfd2 = readfd;
// read inputs from the keyboard
if( FD_ISSET(0,&readfd) )
{
if( read(0,w_buffer,BUF_SIZE) == -1 )
{
perror("read");
return -1;
}
if((size_s= sendto(sock,w_buffer, BUF_SIZE, 0,
(struct sockaddr *)&(tab_client->server),
tab_client->recsize) ) == -1)
{
fprintf(stderr,"Error sendto : Fail to send last message\n\r");
printf("errno %d\n",errno);
close(sock);
return -1;
}
memset(w_buffer,'\0',BUF_SIZE);
}
readfd = readfd2;
// read inputs from the server socket
if (FD_ISSET(sock,&readfd))
{
if((size_r = recvfrom(sock, r_buffer, BUF_SIZE, 0,
(struct sockaddr*)&(tab_client->client),
&(tab_client->recsize))) == -1)
{
perror("Error recvfrom");
printf(" recvfrom errno %d\n",errno);
close(sock);
return -1;
}
if (size_r == 0)
{
fprintf(stderr,"Server disconnected\n\r");
return 1;
}
else
{
// display message
printf(" r_buffer----> %s",r_buffer);
memset(r_buffer,'\0',BUF_SIZE);
}
}
}
return 1;
} |
Partager