-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_tcp_client.c
43 lines (34 loc) · 1.01 KB
/
socket_tcp_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
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h> // for memset()
int main(int argc, char **argv) {
int loop;
char destIP[80];
unsigned short port = 9880;
int destSocket;
struct sockaddr_in destAddr;
char *toSendText = "test message";
if (argc < 2) {
printf("[cmd] [dest IP addr]\n");
printf("e.g. ./a.out 127.0.0.1\n");
return;
}
strcpy(destIP, argv[1]);
printf("to %s\n", destIP);
memset(&destAddr, 0, sizeof(destAddr));
destAddr.sin_addr.s_addr = inet_addr(destIP);
destAddr.sin_port = htons(port);
destAddr.sin_family = AF_INET;
destSocket = socket(AF_INET, SOCK_STREAM, 0);
connect(destSocket, (struct sockaddr *) &destAddr, sizeof(destAddr));
for(loop=0; loop<3; loop++) {
printf("tx:%s\n", toSendText);
send(destSocket, toSendText, strlen(toSendText)+1, 0);
sleep(1);
}
close(destSocket);
}