-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPCLI.C
69 lines (56 loc) · 1.47 KB
/
TCPCLI.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
int main( argc, argv )
int argc;
char **argv;
{
char *tAddrStr = "192.168.42.139";
unsigned long tAddr = inet_addr( tAddrStr );
struct sockaddr_in tDestAddr;
int tSocket;
int tStatus,tSize;
int tMsgCount = 1;
char tXtaBuffer[132];
char tRcvBuffer[132];
tSocket = socket(AF_INET, SOCK_STREAM, 0);
if (tSocket < 0) {
printf("Failed to allocate socket\n");
exit(0);
}
tDestAddr.sin_family = AF_INET;
tDestAddr.sin_port = htons(55555);
tDestAddr.sin_addr.s_addr = (unsigned int) tAddr;
tStatus = connect( tSocket, (struct sockaddr *) &tDestAddr, sizeof( tDestAddr));
if (tStatus < 0)
{
fprintf(stdout, "failed to connect");
exit(0);
}
printf( "Successfully connected\n");
while( 1 == 1) {
sprintf( tXtaBuffer,"Message number %d", tMsgCount++);
printf("write message\n");
tSize = write( tSocket, tXtaBuffer, strlen( tXtaBuffer));
printf("sent message %d\n", tSize);
if (tSize < 0) {
printf("*** Send failure ***\n");
exit(0);
}
tSize = read( tSocket, tRcvBuffer, sizeof( tRcvBuffer));
if (tSize <= 0) {
printf("*** Read failure ***\n");
exit(0);
}
tRcvBuffer[ tSize ] = 0;
printf("[read] %s\n", tRcvBuffer );
sleep(2);
}
printf( "Bye \n");
exit(0);
}