int socket(int domain, int type, int protocol);
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
#### sa_family_t sin_family; /* address family: AF_INET */
#### in_port_t sin_port; /* port in network byte order */
#### struct in_addr sin_addr; /* internet address */
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
//server.c
#include
#include
#include
#include
#include
#include
int StartUp(int port,const char* ip)
{
int ListenSock = socket(AF_INET,SOCK_STREAM,0);
if(ListenSock < 0)
{
perror("socket");
exit(1);
}
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(ip);
if(bind(ListenSock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(2);
}
if(listen(ListenSock,5) < 0)
{
perror("listen");
exit(3);
}
return ListenSock;
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
printf("input error\n");
return 1;
}
int len;
int listenSock = StartUp(atoi(argv[2]),argv[1]);
struct sockaddr_in client;
while(1)
{
int sock = accept(listenSock,(struct sockaddr*)&client,&len);//获取客户机的信息
if(sock < 0)
{
perror("accept");
continue;
}
printf("get a client,ip is %s,port is %d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
char buf[1024];
while(1)
{
ssize_t s = read(sock,buf,sizeof(buf)-1);//服务器进行读数据
if(s > 0)
{
buf[s] = 0;
printf("client# %s\n",buf);
}
else
{
//数据已经读完了,客户端不发送数据了
printf("client is quit!\n");
}
}
close(sock);
}
return 0;
}
//client.c
#include
#include
#include
#include
#include
int main(int argc,const char* argv[])
{
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0)
{
perror("connect");
return 2;
}
char buf[1024];
while(1)
{
printf("send# ");
fflush(stdout);
//从标准输入读数据,读到buf中,然后从buf写到管道
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s < 0)
{
perror("read");
return 3;
}
buf[s-1] = 0;
write(sock,buf,s);
}
close(sock);
return 0;
}
//server.c
#include
#include
#include
#include
#include
#include
int StartUp(int port,const char* ip)
{
int ListenSock = socket(AF_INET,SOCK_STREAM,0);
if(ListenSock < 0)
{
perror("socket");
exit(1);
}
int opt = 1;
setsockopt(ListenSock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(ip);
if(bind(ListenSock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(2);
}
if(listen(ListenSock,5) < 0)
{
perror("listen");
exit(3);
}
return ListenSock;
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
printf("input error\n");
return 1;
}
int len;
int listenSock = StartUp(atoi(argv[2]),argv[1]);
struct sockaddr_in client;
while(1)
{
int sock = accept(listenSock,(struct sockaddr*)&client,&len);//获取客户机的信息
if(sock < 0)
{
perror("accept");
continue;
}
printf("get a client,ip is %s,port is %d\n",inet_ntoa(client.sin_addr),\
ntohs(client.sin_port));
int id = fork();
if(id > 0)
{
close(sock);
while(waitpid(-1,NULL,WNOHANG) > 0);
continue;
}
else
{
close(listenSock);
if(fork() > 0)
{
exit(0);
}
char buf[1024];
while(1)
{
ssize_t s = read(sock,buf,sizeof(buf)-1);//服务器进行读数据
if(s > 0)
{
buf[s] = 0;
printf("client# %s\n",buf);
}
else
{
//数据已经读完了,客户端不发送数据了
printf("client is quit!\n");
break;
}
}
close(sock);
// exit(4);
break;
}
}
return 0;
}
//client.c
#include
#include
#include
#include
#include
int main(int argc,const char* argv[])
{
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
return 1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0)
{
perror("connect");
return 2;
}
char buf[1024];
while(1)
{
printf("send# ");
fflush(stdout);
//从标准输入读数据,读到buf中,然后从buf写到管道
ssize_t s = read(0,buf,sizeof(buf)-1);
if(s < 0)
{
perror("read");
break;
}
buf[s-1] = 0;
write(sock,buf,s);
}
close(sock);
return 0;
}
//server.c
#include
#include
#include
#include
#include
#include
int StartUp(int port,const char* ip)
{
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
exit(2);
}
int opt = 1;
setsockopt(ListenSock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(ip);
if(bind(sock,(struct sockaddr*)&local,sizeof(local)) < 0)
{
perror("bind");
exit(3);
}
if(listen(sock,5) < 0)
{
perror("listen");
exit(4);
}
return sock;
}
void* thread_hander(void* arg)
{
int sock = *((int*)arg);
char buf[1024];
while(1)
{
ssize_t _s = read(sock,buf,sizeof(buf)-1);
if(_s > 0)
{
buf[_s-1] = 0;
printf("client say#%s\n",buf);
if(write(sock,buf,sizeof(buf)-1)<0)
{
break;
}
}
else if(_s == 0)
{
printf("client is quit!\n");
break;
}
else
{
perror("read");
break;
}
}
close(sock);
}
int main(int argc,const char* argv[])
{
if(argc != 3)
{
printf("input error\n");
return 1;
}
int listenSock = StartUp(atoi(argv[2]),argv[1]);
struct sockaddr_in client;
int len = 0;
while(1)
{
int sock = accept(listenSock,(struct sockaddr*)&client,&len);
if(sock < 0)
{
perror("accept");
return 5;
}
printf("get a client!ip is %s,port is %d\n",inet_ntoa(client.sin_addr),\
ntohs(client.sin_port));
pthread_t tid;
int ret = pthread_create(&tid,NULL,thread_hander,&sock);
if(ret < 0)
{
perror("pthread_create");
return 6;
}
pthread_detach(tid);
}
return 0;
}
//client.c
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
if(argc != 3)
{
printf("input error\n");
return 1;
}
int sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
perror("socket");
return 2;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
int ret = connect(sock,(struct sockaddr*)&server,sizeof(server));
if(connect < 0)
{
perror("connect");
return 3;
}
char buf[1024];
while(1)
{
printf("send#");
fflush(stdout);
ssize_t _s = read(0,buf,sizeof(buf)-1);
if(_s > 0)
{
buf[_s - 1] = 0;
if(write(sock,buf,sizeof(buf)-1) < 0)
{
break;
}
ssize_t s = read(sock,buf,sizeof(buf)-1);
if(s > 0)
{
buf[s] = 0;
printf("server echo#%s\n",buf);
}
}
else
{
perror("read");
return 4;
}
}
return 0;
}