#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
void pth_cli(void* arg){
int newfd=*((int *)arg)
ssize_t res=0;
char buf[128]="";
size_t len=sizeof(buf);
while(1){
bzero(buf,sizeof(buf));
res=recv(newfd,buf,len,0);
if(res<0){
ERR_MSG("recv");
return -1;
}else if(0==res){
printf("newfd=%d 客户端关闭\n",newfd);
break;
}
printf("res:%ld nemfd:%d:%s\n",res,newfd,buf);
/*printf("请输入\n");
bzero(cuf,sizeof(cuf));
scanf("%s",cuf);
if(send(newfd,cuf,sizeof(cuf),0)<0){
ERR_MSG("send");
return -1;
}*/
}
close(newfd);
pthread_detach(pthread_self());
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0){
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
pthread_t tid;
while(1) {
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
printf("newfd=%d 连接成功\n",newfd);
if(pthread_create(&tid,NULL,pth_cli,(void*)&newfd)<0){
ERR_MSG("pthread_create");
return -1;
}
}
close(sfd);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式域套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
//判断文件是否存在
if(access("./unix",F_OK)==0){
if(unlink("./unix")<0){
ERR_MSG("unlink");
return -1;
}
}
//填充地址信息结构体
struct sockaddr_un sin;
sin.sun_family=AF_UNIX;
strcpy(sin.sun_path,"./unix");
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
struct sockaddr_un cin;
socklen_t addrlen=sizeof(cin);
int newfd;
pid_t pid;
while(1){
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
printf("newfd=%d 连接成功\n",newfd);
//创建一个子进程
pid=fork();
if(pid>0){
//父进程
close(newfd);
}else if(0==pid){
//子进程
ser_child(newfd);
}else{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
int ser_child(int newfd){
ssize_t res=0;
char buf[128]="";
size_t len=sizeof(buf);
while(1){
bzero(buf,sizeof(buf));
res=recv(newfd,buf,len,0);
if(res<0){
ERR_MSG("recv");
return -1;
}else if(0==res){
printf("newfd=%d 客户端关闭\n",newfd);
break;
}
printf("newfd:%d:%s\n",newfd,buf);
}
close(newfd);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define IP "192.168.8.112"
#define PORT 8888
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
//创建流式域套接字
int sfd=socket(AF_UNIX,SOCK_STREAM,0);
//判断文件是否存在
if(access("./unix",F_OK)==0){
if(unlink("./unix")<0){
ERR_MSG("unlink");
return -1;
}
}
//填充地址信息结构体
struct sockaddr_un sin;
sin.sun_family=AF_UNIX;
strcpy(sin.sun_path,"./unix");
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){
ERR_MSG("bind");
return -1;
}
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("服务器监听成功\n");
struct sockaddr_un cin;
socklen_t addrlen=sizeof(cin);
int newfd;
pid_t pid;
while(1){
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
printf("newfd=%d 连接成功\n",newfd);
//创建一个子进程
pid=fork();
if(pid>0){
//父进程
close(newfd);
}else if(0==pid){
//子进程
ser_child(newfd);
}else{
ERR_MSG("fork");
return -1;
}
}
close(sfd);
return 0;
}
int ser_child(int newfd){
ssize_t res=0;
char buf[128]="";
size_t len=sizeof(buf);
while(1){
bzero(buf,sizeof(buf));
res=recv(newfd,buf,len,0);
if(res<0){
ERR_MSG("recv");
return -1;
}else if(0==res){
printf("newfd=%d 客户端关闭\n",newfd);
break;
}
printf("newfd:%d:%s\n",newfd,buf);
}
close(newfd);
return 0;
}