#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <pthread.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line-->%d\n",__LINE__);\
perror(msg);\
}while(0)
void * client_msg(void * arg);
int main(int argc,const char * argv[])
{
//1.创建套接字流,ipv4
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速被重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(6666);
sa.sin_addr.s_addr = inet_addr(argv[1]);
//2.bind[绑定端口和ip]
if(bind(sfd,(struct sockaddr *)&sa,sizeof(sa)) < 0)
{
ERR_MSG("bind");
return -1;
}
//3.listen[讲通信改为监听]
if(listen(sfd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("*************************\n");
//4.accept[客户端连接]
struct sockaddr_in cls;
socklen_t addrlen = sizeof(cls);
pthread_t tid;
while(1)
{
int cfd = accept(sfd,(struct sockaddr *)&cls,&addrlen);
if(cfd <0 )
{
ERR_MSG("accept");
return -1;
}
if(pthread_create(&tid,NULL,client_msg,(void *)&cfd) < 0)
{
fprintf(stderr,"pthead_create--->errpr");
return -1;
}
pthread_detach(tid);
}
close(sfd);
return 0;
}
void * client_msg(void * arg)
{
ssize_t s;
char buf[128]="";
int cfd = *(int *)arg;
while(1)
{
bzero(buf,sizeof(buf));
s = recv(cfd,buf,sizeof(buf),0);
if(s == 0)
{
printf("客户端挂了\n");
break;
}
else if(s > 0)
{
printf("接收的消息是:%s\n",buf);
}
else if(s < 0)
{
ERR_MSG("recv");
return NULL;
}
}
close(cfd);
pthread_exit(NULL);
}
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line-->%d\n",__LINE__);\
perror(msg);\
}while(0)
void client_msg(int cfd);
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
int main(int argc,const char * argv[])
{
//捕获17号信号,回收僵尸进程
__sighandler_t s = signal(SIGCHLD,handler);
if(SIG_ERR == ssys/wait.h)
{
ERR_MSG("signal");
return -1;
}
//1.创建套接字流,ipv4
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速被重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons(6666);
sa.sin_addr.s_addr = inet_addr(argv[1]);
//2.bind[绑定端口和ip]
if(bind(sfd,(struct sockaddr *)&sa,sizeof(sa)) < 0)
{
ERR_MSG("bind");
return -1;
}
//3.listen[讲通信改为监听]
if(listen(sfd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("*************************\n");
//4.accept[客户端连接]
struct sockaddr_in cls;
socklen_t addrlen = sizeof(cls);
while(1)
{
int cfd = accept(sfd,(struct sockaddr *)&cls,&addrlen);
if(cfd <0 )
{
ERR_MSG("accept");
return -1;
}
int pid = fork();
if(pid <0)
{
ERR_MSG("fork");
return -1;
}
else if(pid > 0)
{
close(cfd);
}
else if(pid == 0)
{
close(sfd);
client_msg(cfd);
close(cfd);
exit(0);
}
}
//6.给客户端发送消息send
/* int send_return = send(cfd,buf,sizeof(buf),0);
if(send_return < 0)
{
ERR_MSG("send");
return -1;
}
*/
close(sfd);
return 0;
}
void client_msg(int cfd)
{
ssize_t s;
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
s = recv(cfd,buf,sizeof(buf),0);
if(s == 0)
{
printf("客户端挂了\n");
break;
}
else if(s > 0)
{
printf("接收的消息是:%s\n",buf);
}
else if(s < 0)
{
ERR_MSG("recv");
return;
}
}
}