-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpServer.c
More file actions
50 lines (48 loc) · 1.29 KB
/
tcpServer.c
File metadata and controls
50 lines (48 loc) · 1.29 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
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>
#include<strings.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
int
main ()
{
short int n = 0, m = 0;
int len = 100;
int addrlen;
unsigned short serv_port = 25000;
short sockfd, connfd;
char serv_ip[] = "127.0.0.1";
char mesg[1000];
char *client_add_dotted;
short client_port;
struct sockaddr_in servaddr, clientaddr;
client_add_dotted = (char *) malloc (sizeof (servaddr));
sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf ("failed");
exit (1);
}
bzero (&servaddr, sizeof (servaddr));
servaddr.sin_family = AF_INET;
inet_aton (serv_ip, &servaddr.sin_addr);
servaddr.sin_port = htons (serv_port);
bind (sockfd, (struct sockaddr *) &servaddr, sizeof (servaddr));
listen (sockfd, 1);
connfd = accept (sockfd, (struct sockaddr *) &clientaddr, &addrlen);
n = read (connfd, mesg, 100);
if (n != -1)
printf ("\n\t\t\t***receive success***\n");
else
printf ("\n\t\t\t***receive failure***\n");
m = write (connfd, mesg, n);
if (m != -1)
printf ("\n\t\t\t***send success***\n");
else
printf ("\n\t\t\t***send failure***\n");
close (sockfd);
}