Skip to content

Commit 4971363

Browse files
authored
upload socket code file
1 parent b761903 commit 4971363

12 files changed

+573
-0
lines changed

Makefile

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
all: myapp
2+
3+
# Which compiler
4+
CC = gcc
5+
6+
# Where to install
7+
INSTDIR = /usr/local/bin
8+
9+
# Where are include files kept
10+
INCLUDE = .
11+
12+
# Options for development
13+
#CFLAGS = -g -Wall -ansi
14+
15+
# Options for release
16+
# CFLAGS = -O -Wall -ansi
17+
18+
CFILE = select_server
19+
20+
myapp: main.o
21+
$(CC) -o $(CFILE) $(CFILE).c
22+
23+
main.o: $(CFILE)
24+
$(CC) -I$(INCLUDE) $(CFLAGS) -c $(CFILE)
25+
26+
27+
clean:
28+
-rm $(CFILE)
29+
30+
install: myapp
31+
@if [ -d $(INSTDIR) ]; \
32+
then \
33+
cp ../* $(INSTDIR);\
34+
chmod a+x $(INSTDIR)/;\
35+
chmod og-w $(INSTDIR)/;\
36+
echo "Installed in $(INSTDIR)";\
37+
else \
38+
echo "Sorry, $(INSTDIR) does not exist";\
39+
fi
40+

client2

11.8 KB
Binary file not shown.

client_ex

7.62 KB
Binary file not shown.

client_ex.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
#include <string.h>
6+
#include <netdb.h>
7+
#include <sys/types.h>
8+
#include <netinet/in.h>
9+
#include <sys/socket.h>
10+
#include <arpa/inet.h>
11+
12+
#define MAXDATASIZE 256 // max number of bytes we can get at once
13+
14+
// get sockaddr, IPv4 or IPv6:
15+
void *get_in_addr(struct sockaddr *sa)
16+
{
17+
if (sa->sa_family == AF_INET) {
18+
return &(((struct sockaddr_in*)sa)->sin_addr);
19+
}
20+
return &(((struct sockaddr_in6*)sa)->sin6_addr);
21+
}
22+
23+
int main(int argc, char *argv[])
24+
{
25+
int sockfd, numbytes;
26+
char buf[MAXDATASIZE];
27+
struct addrinfo hints, *servinfo, *p;
28+
int rv;
29+
char s[INET6_ADDRSTRLEN];
30+
char *port;
31+
32+
port = argv[2];
33+
memset(&hints, 0, sizeof hints);
34+
hints.ai_family = AF_UNSPEC;
35+
hints.ai_socktype = SOCK_STREAM;
36+
if ((rv = getaddrinfo(argv[1], port, &hints, &servinfo)) != 0) {
37+
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
38+
return 1;
39+
}
40+
41+
// loop through all the results and connect to the first we can
42+
for(p = servinfo; p != NULL; p = p->ai_next) {
43+
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
44+
perror("client: socket");
45+
continue;
46+
}
47+
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
48+
close(sockfd);
49+
perror("client: connect");
50+
continue;
51+
}
52+
break;
53+
}
54+
55+
if (p == NULL) {
56+
fprintf(stderr, "client: failed to connect\n");
57+
return 2;
58+
}
59+
60+
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
61+
printf("client: connecting to %s\n", s);
62+
freeaddrinfo(servinfo); // all done with this structure
63+
64+
send(sockfd, "GET / \r\n",8,0);
65+
while(1){
66+
memset(buf,0,MAXDATASIZE);
67+
if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
68+
perror("recv");
69+
exit(1);
70+
}
71+
printf("%s",buf);
72+
73+
}
74+
75+
close(sockfd);
76+
return 0;
77+
}
78+

multi_server

11.9 KB
Binary file not shown.

multi_server.c

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
#include <string.h>
6+
#include <sys/types.h>
7+
#include <sys/socket.h>
8+
#include <netinet/in.h>
9+
#include <netdb.h>
10+
#include <arpa/inet.h>
11+
#include <sys/wait.h>
12+
#include <signal.h>
13+
14+
#define PORT "3490" // the port users will be connecting to
15+
#define BACKLOG 10 // how many pending connections queue will hold
16+
#define MAXDATASIZE 100 // max number of bytes we can get at once
17+
18+
void sigchld_handler(int s)
19+
{
20+
while(waitpid(-1, NULL, WNOHANG) > 0);
21+
}
22+
23+
int main(void)
24+
{
25+
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
26+
struct addrinfo hints, *servinfo, *p;
27+
struct sockaddr_in their_addr; // connector's address information
28+
socklen_t sin_size;
29+
int yes=1;
30+
char s[INET6_ADDRSTRLEN];
31+
int rv;
32+
char buf[MAXDATASIZE];
33+
int numbytes;
34+
struct sigaction sa;
35+
36+
memset(&hints, 0, sizeof hints);
37+
hints.ai_family = AF_UNSPEC;
38+
hints.ai_socktype = SOCK_STREAM;
39+
hints.ai_flags = AI_PASSIVE; // use my IP
40+
41+
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
42+
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
43+
return 1;
44+
}
45+
46+
// loop through all the results and bind to the first we can
47+
for(p = servinfo; p != NULL; p = p->ai_next) {
48+
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
49+
perror("server: socket");
50+
continue;
51+
}
52+
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) {
53+
perror("setsockopt");
54+
exit(1);
55+
}
56+
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
57+
close(sockfd);
58+
perror("server: bind");
59+
continue;
60+
}
61+
break;
62+
}
63+
64+
if (p == NULL) {
65+
fprintf(stderr, "server: failed to bind\n");
66+
return 2;
67+
}
68+
69+
freeaddrinfo(servinfo); // all done with this structure
70+
71+
if (listen(sockfd, BACKLOG) == -1) {
72+
perror("listen");
73+
exit(1);
74+
}
75+
76+
printf("server: waiting for connections...\n");
77+
sin_size = sizeof their_addr;
78+
79+
sa.sa_handler = sigchld_handler; // reap all dead processes
80+
sigemptyset(&sa.sa_mask);
81+
sa.sa_flags = SA_RESTART;
82+
83+
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
84+
perror("sigaction");
85+
exit(1);
86+
}
87+
88+
while(1){
89+
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
90+
if (new_fd == -1) {
91+
// perror("accept");
92+
return;
93+
}
94+
95+
inet_ntop(their_addr.sin_family,(struct sockaddr *)&their_addr.sin_addr,s, sizeof s);
96+
printf("server: got connection from %s port: %d\n", s,their_addr.sin_port);
97+
98+
if (!fork()) { // this is the child process
99+
close(sockfd); // child doesn't need the listener
100+
if ((numbytes = recv(new_fd, buf, 256, 0)) == -1) {
101+
perror("recv");
102+
exit(1);
103+
}else if (numbytes == 0) {
104+
// connection closed
105+
printf("Client socket %s hung up\n",s );
106+
}else{
107+
printf("received:%s",buf);
108+
if (send(new_fd, buf, numbytes, 0) == -1) {
109+
perror("send");
110+
}
111+
}
112+
printf("Client socket %s closed\n",s );
113+
}
114+
close(new_fd); // parent doesn't need this
115+
}
116+
close(sockfd); // listen socket
117+
return 0;
118+
}

select_server

12.1 KB
Binary file not shown.

select_server.c

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
#include <string.h>
6+
#include <sys/types.h>
7+
#include <sys/socket.h>
8+
#include <netinet/in.h>
9+
#include <netdb.h>
10+
#include <arpa/inet.h>
11+
#include <sys/wait.h>
12+
#include <signal.h>
13+
14+
#define PORT "3490" // the port users will be connecting to
15+
#define BACKLOG 10 // how many pending connections queue will hold
16+
#define MAXDATASIZE 100 // max number of bytes we can get at once
17+
18+
void sigchld_handler(int s)
19+
{
20+
while(waitpid(-1, NULL, WNOHANG) > 0);
21+
}
22+
23+
int main(void)
24+
{
25+
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
26+
struct addrinfo hints, *servinfo, *p;
27+
struct sockaddr_in their_addr; // connector's address information
28+
socklen_t sin_size;
29+
int yes=1;
30+
char s[INET6_ADDRSTRLEN];
31+
int rv;
32+
char buf[MAXDATASIZE];
33+
int numbytes;
34+
struct sigaction sa;
35+
pid_t pid;
36+
int err;
37+
int i;
38+
int fdmax; // maximum file descriptor number
39+
fd_set master; // master file descriptor list
40+
fd_set read_fds; // temp file descriptor list for select()
41+
42+
43+
memset(&hints, 0, sizeof hints);
44+
hints.ai_family = AF_UNSPEC;
45+
hints.ai_socktype = SOCK_STREAM;
46+
hints.ai_flags = AI_PASSIVE; // use my IP
47+
48+
if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
49+
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
50+
return 1;
51+
}
52+
53+
// loop through all the results and bind to the first we can
54+
for(p = servinfo; p != NULL; p = p->ai_next) {
55+
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
56+
perror("server: socket");
57+
continue;
58+
}
59+
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) {
60+
perror("setsockopt");
61+
exit(1);
62+
}
63+
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
64+
close(sockfd);
65+
perror("server: bind");
66+
continue;
67+
}
68+
break;
69+
}
70+
71+
if (p == NULL) {
72+
fprintf(stderr, "server: failed to bind\n");
73+
return 2;
74+
}
75+
76+
freeaddrinfo(servinfo); // all done with this structure
77+
78+
if (listen(sockfd, BACKLOG) == -1) {
79+
perror("listen");
80+
exit(1);
81+
}
82+
83+
printf("server: waiting for connections...\n");
84+
sin_size = sizeof their_addr;
85+
86+
sa.sa_handler = sigchld_handler; // reap all dead processes
87+
sigemptyset(&sa.sa_mask);
88+
sa.sa_flags = SA_RESTART;
89+
90+
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
91+
perror("sigaction");
92+
exit(1);
93+
}
94+
95+
while(1){
96+
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
97+
if (new_fd == -1) {
98+
// perror("accept");
99+
return;
100+
}
101+
102+
inet_ntop(their_addr.sin_family,(struct sockaddr *)&their_addr.sin_addr,s, sizeof s);
103+
printf("server: got connection from %s port: %d\n", s,their_addr.sin_port);
104+
105+
pid = fork();
106+
if (!pid) { // this is the child process
107+
close(sockfd); // child doesn't need the listener
108+
109+
while(1){
110+
FD_ZERO(&master); // clear the master and temp sets
111+
FD_ZERO(&read_fds);
112+
FD_SET(new_fd, &master); // add to master set
113+
FD_SET(0, &master); // add to master set
114+
fdmax = new_fd;
115+
read_fds = master; // copy it
116+
117+
restart_select:
118+
if ((err = select(fdmax+1, &read_fds, NULL, NULL, NULL)) == -1) {
119+
if(err == EINTR){
120+
goto restart_select;
121+
}
122+
// perror("select");
123+
exit(4);
124+
}
125+
for(i = 0; i <= fdmax; i++) {
126+
if (FD_ISSET(i, &read_fds)) { // catched
127+
if(i == new_fd){ // data received
128+
memset(buf,0,MAXDATASIZE);
129+
if ((numbytes = recv(new_fd, buf, 256, 0)) == -1) {
130+
perror("recv");
131+
exit(1);
132+
}else if (numbytes == 0) {
133+
// connection closed
134+
printf("Client socket %s hung up\n",s );
135+
close(new_fd); // parent doesn't need this
136+
return 0;
137+
}else{
138+
printf("RCV [%s] :%s",s,buf);
139+
}
140+
}else if(i == 0){
141+
memset(buf,0,MAXDATASIZE);
142+
fgets(buf,MAXDATASIZE,stdin);
143+
printf("SND: %s\n", buf);
144+
if (send(new_fd, buf,strlen(buf) , 0) == -1) {
145+
perror("send");
146+
}
147+
// printf("send data: %s",buf);
148+
}
149+
}
150+
}
151+
}
152+
}
153+
// printf("Client socket %s closed\n",s );
154+
close(new_fd); // parent doesn't need this
155+
}
156+
close(sockfd); // listen socket
157+
return 0;
158+
}

simple_client

7.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)