-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.c
132 lines (113 loc) · 2.77 KB
/
news.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#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 <netdb.h>
#define PORT 4444
#define BUFSIZE 1024
void send_to_all(int j, int i, int sockfd, int nbytes_recvd, char *recv_buf, fd_set *master)
{
if (FD_ISSET(j, master)){
if (j != sockfd && j != i) {
if (send(j, recv_buf, nbytes_recvd, 0) == -1) {
perror("send");
}
}
}
}
void send_recv(int i, fd_set *master, int sockfd, int fdmax)
{
int nbytes_recvd, j;
char recv_buf[BUFSIZE], buf[BUFSIZE];
if ((nbytes_recvd = recv(i, recv_buf, BUFSIZE, 0)) <= 0) {
if (nbytes_recvd == 0) {
printf("socket %d hung up\n", i);
}else {
perror("recv");
}
close(i);
FD_CLR(i, master);
}else {
printf("%s\n",recv_buf);
// printf("from");
//printf("%s",inet_ntoa(client_addr->sin_addr));
for(j = 0; j <= fdmax; j++){
send_to_all(j, i, sockfd, nbytes_recvd, recv_buf, master );
}
}
}
void connection_accept(fd_set *master, int *fdmax, int sockfd, struct sockaddr_in *client_addr)
{
socklen_t addrlen;
int newsockfd;
addrlen = sizeof(struct sockaddr_in);
if((newsockfd = accept(sockfd, (struct sockaddr *)client_addr, &addrlen)) == -1) {
perror("accept");
exit(1);
}else {
FD_SET(newsockfd, master);
if(newsockfd > *fdmax){
*fdmax = newsockfd;
}
printf("new connection from %s on port %d \n",inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
}
}
void connect_request(int *sockfd, struct sockaddr_in *my_addr)
{
int yes = 1;
if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
my_addr->sin_family = AF_INET;
my_addr->sin_port = htons(4444);
my_addr->sin_addr.s_addr = INADDR_ANY;
memset(my_addr->sin_zero, '\0', sizeof my_addr->sin_zero);
if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(*sockfd, (struct sockaddr *)my_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
if (listen(*sockfd, 10) == -1) {
perror("listen");
exit(1);
}
printf("\nTCPServer Waiting for client on port 4444\n");
fflush(stdout);
}
int main()
{
fd_set master;
fd_set read_fds;
int fdmax, i;
int sockfd= 0;
struct sockaddr_in my_addr, client_addr;
FD_ZERO(&master);
FD_ZERO(&read_fds);
connect_request(&sockfd, &my_addr);
FD_SET(sockfd, &master);
fdmax = sockfd;
while(1){
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){
perror("select");
exit(4);
}
for (i = 0; i <= fdmax; i++){
if (FD_ISSET(i, &read_fds)){
if (i == sockfd)
connection_accept(&master, &fdmax, sockfd, &client_addr);
else
send_recv(i, &master, sockfd, fdmax);
}
}
}
return 0;
}