-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
43 lines (33 loc) · 1 KB
/
client.c
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
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *host = "127.0.0.1";
int port = 2000;
int timeout = 2;
int sockfd, n;
char buffer[256];
struct sockaddr_in serv_addr;
struct hostent *server;
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
server = gethostbyname(host);
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(port);
serv_addr.sin_family = AF_INET;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval));
connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
n = read(sockfd, buffer, 255);
if (n < 0) {
perror("error reading from socket");
return 1;
}
printf("%s\n", buffer);
return 0;
}