目录
127.0.0.1 (本地地址)系统不同,本地地址不同。
1.socket 服务器实现不断接收客户端信息
做法:用一个while(1) 死循环,使得 socket 服务器不断接收accept ( ) 客户端的数据。
当客户端有数据接入,调用fork( ) 函数创建子进程去处理数据。
server.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128];
char *msg = "I got your message";
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){ //判断一下参数,否则运行参数没写对的时候,出现段错误不知道是哪里搞错。
printf("param isn't right\n");
}
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socked");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&s_addr.sin_addr);
//2.bind
bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int clen = sizeof(struct sockaddr_in);
while(1){
c_fd = accept(s_fd,(struct sockaddr *)&c_addr, &clen);
if(c_fd == -1){
perror("accept");
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0){
//5.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message: %d ,%s\n",n_read,readBuf);
}
//6.write
write(c_fd,msg,strlen(msg));
break; //子进程做完一次就退出,父进程继续接收数据
}
}
return 0;
}
~
client.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
char *msg = "from client msg";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){ //判断一下参数,否则参数没写对的时候,出现段错误不知道是哪里搞错。
printf("param isn't right\n");
}
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&c_addr.sin_addr);
// memset(&c_addr,0,sizeof(struct sockaddr_in)); (error:清空位置不对)
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(-1);
}
//3.send
int n_write = write(c_fd,msg,strlen(msg));
if(n_write == -1){
perror("write:");
}
//4.read
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message from server: %d ,%s\n",n_read,readBuf);
}
return 0;
}
运行结果:
2.socket 服务器接收多个客户端数据
说明:由于水平原因,这里做到的是服务器接收多个客户端发送的消息,而无法做到服务器能准确回复哪个客户端进程消息。
【但是:一个服务器对应一个客户端作双方交互信息还是可以做到的,稍稍改一下 write()接收键盘输入发送回去 就行】
server.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128];
int mark = 0;
char msg[128] = {0};
// char *msg = "I got your message";
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param isn't right\n");
}
//1.socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("socked");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&s_addr.sin_addr);
//2.bind
bind(s_fd, (struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
//3.listen
listen(s_fd,10);
//4.accept
int clen = sizeof(struct sockaddr_in);
while(1){
c_fd = accept(s_fd,(struct sockaddr *)&c_addr, &clen);
if(c_fd == -1){
perror("accept");
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
mark++;
if(fork() == 0){
if(fork() == 0){
while(1){
//6.write
memset(msg,0,sizeof(msg));
sprintf(msg,"welcome %d client",mark); //将回复的信息直接放到msg 回复
write(c_fd,msg,strlen(msg));
sleep(3);
}
}
//5.read
while(1){
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("\nget:%s\n",readBuf);
}
}
break;
}
}
return 0;
}
client.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128];
char msg[128] = {0};
// char *msg = "from client msg";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3){
printf("param isn't right\n");
}
//1.socket
c_fd = socket(AF_INET,SOCK_STREAM,0);
if(c_fd == -1){
perror("socket");
exit(-1);
}
c_addr.sin_family = AF_INET;
c_addr.sin_port = htons(atoi(argv[2]));
//s_addr.sin_addr.s_addr = "127.0.0.1"; (error)
inet_aton(argv[1],&c_addr.sin_addr);
// memset(&c_addr,0,sizeof(struct sockaddr_in)); (error:清空位置不对)
//2.connect
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in)) == -1){
perror("connect");
exit(-1);
}
while(1){
if(fork() == 0){
while(1){
//3.send
memset(msg,0,sizeof(msg));
printf("input: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
while(1){
//4.read
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("\nget: %s\n",readBuf);
}
}
}
return 0;
}
一个客户端运行结果: