-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost.c
More file actions
73 lines (50 loc) · 1.32 KB
/
Copy pathhost.c
File metadata and controls
73 lines (50 loc) · 1.32 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
68
69
70
71
72
73
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
// the host is in pure C without a Mex interface.
#define NCHARS 500
#define MAX_USERS 10
typedef struct connected_users{
int n_users;
str *user_addresses[MAX_USERS];
};
void echo(int PORTNUM){
char str[NCHARS];
int listen_fd, comm_fd;
struct sockaddr_in servaddr;
listen_fd = socket(AF_INET, SOCK_STREAM,0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htons(INADDR_ANY);
servaddr.sin_port = htons(PORTNUM);
printf("Listening on port %d\n", PORTNUM);
bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
// printf("Listening\n");
listen(listen_fd, MAX_USERS);
comm_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
printf("Connected\n");
while(1)
{
bzero(str, NCHARS);
read(comm_fd, str, NCHARS);
if(strcmp(str, "")){
printf("Echoing back - %s\n", str);
write(comm_fd, str, strlen(str)+1);
}
}
}
int main(int argc, char* argv[]){
int PORTNUM;
struct
if(argc == 2){
PORTNUM = atoi(argv[1]);
}
else{
PORTNUM = 22000;
}
echo(PORTNUM);
}