#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/wait.h>
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#define SER_IP "192.168.203.255"
#define SER_PORT 8888
void handler(int signo)
{
if(signo == SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
}
int main(int argc, const char *argv[])
{
if(signal(SIGCHLD,handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success sfd = %d\n",sfd);
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if( bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if( listen(sfd, 128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("[%s:%d]:已连接,newfd = %d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
pid_t pid = fork();
if(pid > 0)
{
close(newfd);
}else if(pid == 0)
{
close(sfd);
char rbuf[128] = "";
while(1)
{
//int ret = read(newfd, rbuf, sizeof(rbuf));
int ret = recv(newfd, rbuf, sizeof(rbuf), 0);
if(ret == 0)
{
printf("客户端已下线\n");
break;
}
printf("[%s:%d]: %s\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),rbuf );
strcat(rbuf, "*_*");
//write(newfd, rbuf, strlen(rbuf));
send(newfd, rbuf, strlen(rbuf), 0);
printf("发送成功\n");
}
close(newfd);
exit(EXIT_SUCCESS);
}
}
close(sfd);
return 0;
}
#include <myhead.h>
//多线程实现TCP并发服务器
//线程传参结构体
typedef struct{
int newfd;
struct sockaddr_in cin;
}para_t;
void *task(void *arg);
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
perror("socket");
return -1;
}
//设置端口快速重用
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
perror("setsockopt");
return -1;
}
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(6666);
sin.sin_addr.s_addr=inet_addr("192.168.125.13");
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
perror("bind");
return -1;
}
//listen
listen(sfd,128);
//客户端地址信息结构体
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
while(1){
//接收客户端的连接
int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
perror("accept");
return -1;
}
printf("[%s %d]accept success\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
//创建用于和客户端通信的线程
para_t info={newfd,cin};
pthread_t tid;
if(pthread_create(&tid,NULL,task,&info)!=0){
printf("pthread_create err\n");
return -1;
}
//分离线程
pthread_detach(tid);
}
close(sfd);
return 0;
}
void *task(void *arg){
int newfd=((para_t*)arg)->newfd;
struct sockaddr_in cin=((para_t*)arg)->cin;
char buf[128]="";
while(1){
//接收客户端信息
memset(buf,0,sizeof(buf));
if(0==recv(newfd,buf,sizeof(buf),0)){
printf("[%s %d]客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
break;
}
printf("[%s %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
strcpy(buf,"*-*");
send(newfd,buf,sizeof(buf),0);
}
//关闭用于和客户端通信的套接字,退出线程
close(newfd);
pthread_exit(NULL);
}
#include <myhead.h>
//流式域套接字服务器
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
if(sfd<0){
perror("socket");
return -1;
}
//判断套接字文件是否存在
if(access("./unix",F_OK)==0){
if(unlink("./unix")<0){
perror("unlink");
return -1;
}
}
//填充地址信息结构体
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
//绑定
if(bind(sfd,(struct sockaddr*)&sun,sizeof(sun))<0){
perror("bind");
return -1;
}
//监听
listen(sfd,128);
//客户端地址信息结构体
struct sockaddr_un cun;
socklen_t addrlen=sizeof(cun);
//接受客户端连接
int newfd=accept(sfd,(struct sockaddr*)&cun,&addrlen);
if(newfd<0){
printf("accept");
return -1;
}
printf("11\n");
printf("[%s] accept success\n",cun.sun_path);
char buf[128]="";
while(1){
memset(buf,0,sizeof(buf));
if(0==recv(newfd,buf,sizeof(buf),0)){
printf("[%s]客户端下线\n",cun.sun_path);
break;
}
printf("[%s]:%s\n",cun.sun_path,buf);
strcat(buf,"*-*");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
close(sfd);
return 0;
}
#include <myhead.h>
//报式域套接字
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(sfd<0){
perror("socket");
return -1;
}
//判断套接字文件是否存在,存在则删除
if(access("./unix",F_OK)==0){
if(unlink("./unix")!=0){
perror("unlink");
return -1;
}
}
//填充地址信息结构体
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
//绑定
if(bind(sfd,(struct sockaddr *)&sun,sizeof(sun))<0){
perror("bind");
return -1;
}
//客户端地址信息结构体
struct sockaddr_un cun;
socklen_t addrlen=sizeof(cun);
char buf[128]="";
//接收发送
while(1){
//接收客户端信息
memset(buf,0,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,&addrlen)<0){
perror("recvfrom");
break;
}
printf("[%s]:%s\n",cun.sun_path,buf);
strcat(buf,"*-*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cun,addrlen)<0){
perror("sendto");
break;
}
}
close(sfd);
return 0;
}
#include <myhead.h>
//报式域套接字
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_UNIX,SOCK_DGRAM,0);
if(cfd<0){
perror("socket");
return -1;
}
//判断套接字文件是否存在,存在则删除
if(access("./linux",F_OK)==0){
if(unlink("./linux")!=0){
perror("unlink");
return -1;
}
}
//填充地址信息结构体
struct sockaddr_un cun;
cun.sun_family=AF_UNIX;
strcpy(cun.sun_path,"./linux");
//绑定
if(bind(cfd,(struct sockaddr *)&cun,sizeof(cun))<0){
perror("bind");
return -1;
}
//服务器地址信息结构体
struct sockaddr_un sun;
sun.sun_family=AF_UNIX;
strcpy(sun.sun_path,"./unix");
socklen_t addrlen=sizeof(sun);
char buf[128]="";
//接收发送
while(1){
//从终端获取输入
memset(buf,0,sizeof(buf));
printf("请输入>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
//发送给服务器
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sun,addrlen)<0){
perror("sendto");
break;
}
//接收服务信息
memset(buf,0,sizeof(buf));
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sun,&addrlen)<0){
perror("recvfrom");
break;
}
printf("%s\n",buf);
}
close(cfd);
return 0;
}