Client
//============================================================================
// Name : EchoClient.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <sys/socket.h>
#include <strings.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
const char *IPADRR = "127.0.0.1";
const unsigned int SERVERPORT = 7778;
const unsigned int MAXLINE = 1024;
void doit(FILE *fp, int sockfd)
{
int ret;
char send_buf[MAXLINE], recv_buf[MAXLINE];
while(fgets(send_buf, MAXLINE, fp) != NULL){
if((ret = send(sockfd, send_buf, strlen(send_buf), 0)) == -1){
printf("send error\n");
return;
}
//每次接收数据前将recv_buf清空,主要是填充'\0',因为接收数据不会在添上'\0'为结尾
bzero(recv_buf, MAXLINE);
if((ret = recv(sockfd, recv_buf, MAXLINE, 0)) == 0){
printf("server stopped!\n");
}
fputs(recv_buf, stdout);
}
}
int main() {
int clifd;
struct sockaddr_in serveraddr;
if((clifd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("socket create error\n");
return -1;
}
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(IPADRR);
serveraddr.sin_port = htons(SERVERPORT);
if(connect(clifd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
printf("connect to server fail\n");
return -1;
}
printf("connect to server successfully!\n");
doit(stdin, clifd);
return 0;
}
server
//============================================================================
// Name : EchoServer.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
const unsigned int PORT = 7778;
const unsigned int LISTEN_NUM = 128;
const unsigned int MAXLINE = 1024;
void* doit(void *arg)
{
pthread_detach(pthread_self());
char buf[MAXLINE];
int connfd = (int)arg;
int n = 0;
while((n = recv(connfd, buf, sizeof(buf), 0)) > 0){
send(connfd, buf, n, 0);
}
if(n == -1){
printf("receive error\n");
}
close(connfd);
return NULL;
}
int main() {
struct sockaddr_in cliaddr, serveraddr;
int listenfd, connfd;
socklen_t clilen;
pthread_t tid;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if(bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == -1){
printf("bind error\n");
return -1;
}
if(listen(listenfd,LISTEN_NUM) == -1){
printf("listen error\n");
return -1;
}
for(;;){
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
if(connfd>0){
if(pthread_create(&tid, NULL, doit, (void *)connfd)){
printf("pthread create error\n");
return -1;
}
}
}
close(listenfd);
return 0;
}