Linux教程

Linux 网络编程 实现双方通信

本文主要是介绍Linux 网络编程 实现双方通信,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

//服务端

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int main(int argc,char **argv) //传参 ./server IP 端口号
{
int s_fd;
int c_fd;
int n_read;
char readBuf[128] = {0};
//char *msg = "I get your connect!";
char msg[128] = {0};
struct sockaddr_in s_addr;
struct sockaddr_in c_addr;

if(argc != 3){
printf("param is not good\n");
exit(-1);
}
memset(&s_addr,0,sizeof(struct sockaddr_in));
memset(&c_addr,0,sizeof(struct sockaddr_in));
//1.int socket(int domain, int type, int protocol);
//创建一个socket
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1){
perror("scoket");
exit(-1);
}
//设置socket地址
s_addr.sin_family = AF_INET; //地址族:使用IPv4
s_addr.sin_port = htons(atoi(argv[2])); //端口号,使用网络字节序表示,atoi()将ASCII强制转换为int
inet_aton(argv[1],&s_addr.sin_addr); //IP地址v4地址,用网络字节序表示

/*struct sockaddr_in
{
sa_family_t sin_family; //地址族:AF_INET
u_int16_t sin_port; //端口号,要使用网络字节序表示
struct in_addr sin_addr; //IPv4地址结构体
char sin_zero[8]; //不使用
}
struct in_addr
{
u_int32_t s_addr; //IPv4地址,要用网络字节序表示
}*/

//分配地址信息,socket命名
//2.int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));

//创建一个监听队列
//3.int listen(int sockfd, int backlog);
listen(s_fd,10);
//连接
//4.int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
int clen = sizeof(struct sockaddr_in);
while(1){ //不断的读取和写入
c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&clen); //连接
if(c_fd == -1){
perror("accept");
exit(-1); //连接失败退出
}
printf("get connect:%s\n",inet_ntoa(c_addr.sin_addr));
if(fork() == 0){ //创建子进程,实现同时运行两个while(1)
//6.write
while(1){
memset(msg,0,sizeof(msg)); //清除原始数据
printf("input: \n");
gets(msg); //没输入时阻塞
write(c_fd,msg,strlen(msg)); //将输入信息存入msg进行交互
}
}
//5.read
while(1){
memset(readBuf,0,sizeof(readBuf)); //清楚原始数据
//ssize_t read(int fd, void *buf, size_t count);
n_read = read(c_fd,readBuf,128); //读取
if(n_read == -1){
perror("read");
}else{
printf("get message:%d,from client:%s\n",n_read,readBuf);
}
}
//7.close
close(s_fd);
close(c_fd);
}
return 0;
}

 

 

 

客户端

 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>

int main(int argc,char **argv)
{
int c_fd;
int n_read;
char readBuf[128] = {0};

// char *msg = "message from client";
char msg[128] = {0};
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));

if(argc != 3){
printf("param error\n");
exit(-1);
}
//1.sockey
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]));
inet_aton(argv[1],&c_addr.sin_addr);

//4.connect
int c_net = connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr));
if(c_net == 1){
perror("connect");
exit(-1);
}

while(1){
if(fork() == 0){
while(1){
//5.send
memset(msg,0,sizeof(msg));
printf("input data: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
while(1){
//6.read
memset(readBuf,0,sizeof(readBuf));
n_read = read(c_fd,readBuf,128);
if(n_read == -1){
perror("read");
}else{
printf("get message form server:%d,%s\n",n_read,readBuf);
}
}
}
//7.close
close(c_fd);
return 0;
}

这篇关于Linux 网络编程 实现双方通信的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!