-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCPEchoClient4.c
More file actions
executable file
·67 lines (56 loc) · 2.02 KB
/
Copy pathTCPEchoClient4.c
File metadata and controls
executable file
·67 lines (56 loc) · 2.02 KB
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
//
// Created by tw on 2018/2/27.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Practical.h"
int main(int argc, char *argv[])
{
if (argc < 3||argc > 4)
DieWithUserMessage("Parameter(s)", "<Server Address> <Echo Word> [<Server Port>]");
char *servIP = argv[1];
char *echoString = argv[2];
in_port_t servPort = (argc == 4) ? atoi(argv[3]) : 7;
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
DieWithSystemMessage("socket() failed");
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
int rthVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr);
if (rthVal == 0)
DieWithUserMessage("inet_pton() failed", "invalid address string");
else if (rthVal < 0)
DieWithSystemMessage("inet_pton() failed");
servAddr.sin_port = htons(servPort);
if (connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
DieWithSystemMessage("connect() failed");
size_t echoStringLen = strlen(echoString);
ssize_t numBytes = send(sock, echoString, echoStringLen, 0);
if (numBytes < 0)
DieWithSystemMessage("send() failed");
else if (numBytes != echoStringLen)
DieWithUserMessage("send()", "sent unexpected number of bytes");
unsigned int totalBytesRcvd = 0;
fputs("Received: ", stdout);
while (totalBytesRcvd < echoStringLen) {
char buffer[BUFSIZ];
numBytes = recv(sock, buffer, BUFSIZ-1, 0);
if (numBytes < 0)
DieWithSystemMessage("recv() failed");
else if (numBytes == 0)
DieWithUserMessage("recv()", "connection cloese prematurely");
totalBytesRcvd += numBytes;
buffer[numBytes] = '\0';
fputs(buffer, stdout);
}
fputs("\n", stdout);
close(sock);
exit(0);
}