2016-06-14 16:01:19 -07:00
|
|
|
/*
|
|
|
|
|
C ECHO client example using sockets
|
|
|
|
|
*/
|
|
|
|
|
#include<stdio.h> //printf
|
|
|
|
|
#include<string.h> //strlen
|
|
|
|
|
#include<sys/socket.h> //socket
|
|
|
|
|
#include<arpa/inet.h> //inet_addr
|
|
|
|
|
|
|
|
|
|
int main(int argc , char *argv[])
|
|
|
|
|
{
|
2016-06-20 12:37:14 -07:00
|
|
|
if(argc < 3) {
|
|
|
|
|
printf("usage: client <addr> <port>\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sock, port = atoi(argv[1]);
|
2016-06-14 16:01:19 -07:00
|
|
|
struct sockaddr_in server;
|
|
|
|
|
char message[1000] , server_reply[2000];
|
|
|
|
|
|
|
|
|
|
sock = socket(AF_INET , SOCK_STREAM , 0);
|
2016-06-20 12:37:14 -07:00
|
|
|
if (sock == -1) {
|
2016-06-14 16:01:19 -07:00
|
|
|
printf("Could not create socket");
|
2016-06-20 12:37:14 -07:00
|
|
|
}
|
|
|
|
|
server.sin_addr.s_addr = inet_addr(argv[1]);
|
2016-06-14 16:01:19 -07:00
|
|
|
server.sin_family = AF_INET;
|
2016-06-20 12:37:14 -07:00
|
|
|
server.sin_port = htons( port );
|
2016-06-14 16:01:19 -07:00
|
|
|
|
2016-06-20 12:37:14 -07:00
|
|
|
printf("connecting...\n");
|
2016-06-14 16:01:19 -07:00
|
|
|
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
|
|
|
|
|
{
|
|
|
|
|
perror("connect failed. Error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
puts("Connected\n");
|
|
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
printf("Enter message : ");
|
|
|
|
|
scanf("%s" , message);
|
|
|
|
|
|
|
|
|
|
//Send some data
|
|
|
|
|
if(send(sock , message , strlen(message) , 0) < 0)
|
|
|
|
|
{
|
|
|
|
|
puts("Send failed");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Receive a reply from the server
|
|
|
|
|
if(recv(sock , server_reply , 2000 , 0) < 0)
|
|
|
|
|
{
|
|
|
|
|
puts("recv failed");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
puts("Server reply :");
|
|
|
|
|
puts(server_reply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(sock);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|