forked from jasonxuuuu/MyCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Wuchenwcf
committed
Aug 15, 2017
1 parent
68115ae
commit a63a126
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* TCP回射客户程序*/ | ||
|
||
#include<unistd.h>//for read write | ||
#include<netinet/in.h> //for sockaddr_in | ||
#include<arpa/inet.h> //for inet_pton | ||
#include<stdio.h> //for stdion stdout | ||
#include<string.h> | ||
#include<iostream> | ||
|
||
using namespace std; | ||
#define MAXLINE 100 | ||
|
||
int SERVER_PORT = 21; | ||
char SERVER_ADDR[] = "10.8.225.94"; | ||
|
||
void str_cli(FILE *fp, int sockfd) | ||
{ | ||
char sendline[MAXLINE], recvline[MAXLINE]; | ||
|
||
while (fgets(sendline,MAXLINE,fp)!=NULL) | ||
{ | ||
// cout<<"fgets success!"<<endl; | ||
write(sockfd, sendline, strlen(sendline)); | ||
// cout<<"write success!"<<endl; | ||
if (read(sockfd, recvline, MAXLINE) == 0) | ||
{ | ||
cout << "server terminated prematurely"; | ||
} | ||
// cout<<"read success!"<<endl; | ||
fputs(recvline, stdout); | ||
|
||
} | ||
// cout<<"fgets error"<<endl; | ||
|
||
} | ||
|
||
int main() | ||
{ | ||
int sockfd; | ||
sockaddr_in servaddr; | ||
sockfd = socket(AF_INET, SOCK_STREAM, 0); | ||
bzero(&servaddr, sizeof(servaddr)); | ||
servaddr.sin_family = AF_INET; | ||
servaddr.sin_port = htons(SERVER_PORT); | ||
inet_pton(AF_INET, SERVER_ADDR, &servaddr.sin_addr); | ||
connect(sockfd, (sockaddr *)&servaddr, sizeof(servaddr)); | ||
str_cli(stdin, sockfd); | ||
// cout<<"error"<<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* TCP回射服务端程序*/ | ||
|
||
|
||
#include<unistd.h>//for read write | ||
#include<netinet/in.h> //for sockaddr_in | ||
#include<arpa/inet.h> //for inet_pton | ||
#include<stdio.h> //for stdion stdout | ||
#include<errno.h> //for errno | ||
#include<string.h> | ||
#include<iostream> | ||
|
||
using namespace std; | ||
#define MAXLINE 100 | ||
int SERVER_PORT = 21; | ||
int LISTENQ = 20; | ||
|
||
void str_echo(int sockfd) | ||
{ | ||
ssize_t n; | ||
char buf[MAXLINE]; | ||
|
||
again: | ||
while ((n=read(sockfd,buf,MAXLINE))>0) | ||
{ | ||
write(sockfd, buf, n); | ||
|
||
if (n < 0 && errno == EINTR) | ||
goto again; | ||
else if(n<0) | ||
{ | ||
cout << "error" << endl; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int listenfd, connfd; | ||
pid_t childpid; | ||
socklen_t client; | ||
sockaddr_in cliaddr, servaddr; | ||
listenfd = socket(AF_INET, SOCK_STREAM, 0); | ||
bzero(&servaddr, sizeof(servaddr)); | ||
servaddr.sin_family = AF_INET; | ||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
servaddr.sin_port = htons(SERVER_PORT); | ||
bind(listenfd, (sockaddr *)&servaddr, sizeof(servaddr)); | ||
listen(listenfd, LISTENQ); | ||
while (true) | ||
{ | ||
client = sizeof(cliaddr); | ||
connfd = accept(listenfd, (sockaddr *)&servaddr, &client); | ||
if ((childpid = fork()) == 0) | ||
{ | ||
close(listenfd); | ||
str_echo(connfd); | ||
// cout<<"child has been closed!"<<endl; | ||
exit(0); | ||
//子进程结束后,依然处于僵死状态,占用系统资源 | ||
} | ||
close(connfd);//如果不关闭,则会一直处于close_wait状态 | ||
} | ||
return 0; | ||
} |