-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
49 lines (48 loc) · 1.25 KB
/
tcp.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
44
45
46
47
48
49
#include "err.h"
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
int TCP_connect(const char *servIP, int servPort)
{
int sockfd;
struct sockaddr_in servaddr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
errExit("socket");
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(servPort);
if (inet_pton(AF_INET, servIP, &servaddr.sin_addr) != 1)
errExit("inet_pton");
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
errExit("connect");
return sockfd;
}
void TCP_send(int sockfd, const char *buf, size_t nbytes)
{
ssize_t count = write(sockfd, buf, nbytes);
if (count != nbytes)
errExit("write");
}
ssize_t TCP_recv(int sockfd, char *buf, size_t nbytes)
{
ssize_t sum, count;
for (sum = 0;;) {
count = read(sockfd, buf + sum, nbytes);
if (count == -1)
errExit("read");
sum += count;
if (buf[sum - 1] == '}')
break;
else
sleep(1);
}
return sum;
}
/*
ssize_t TCP_recv(int sockfd, char *buf, size_t nbytes)
{
ssize_t count = read(sockfd, buf, nbytes);
if (count == -1)
errExit("read");
return count;
}*/