Linux网络编程——Socket相关代码实战
2023-12-26 06:50:45
客户端代码实现
相关函数如下
代码编程时也应按一下顺序:
(1)socket函数
int socket(int domain,int type, int protocol);
(2)connect函数
int connect (int sockfd, struct sockaddr *addr, socklen_t addrlen);
(3)write函数
ssize_t write(int fd,void *buf,size_t nbyte);
(4)raad函数
ssize_t read(int fd, const void *buf,size_t nbytes);
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int c_fd;
int n_read;
char readBuf[128];
char *msg = "message from FuwuDuan";
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
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(8888);
inet_aton("127.0.0.1",&c_addr.sin_addr);
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect");
exit(-1);
}
write(c_fd,msg,strlen(msg));//先发送到服务端 服务端先read并将其打印
n_read = read(c_fd,readBuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("get message from FuwuDuan:%d,%s\n",n_read,readBuf);
}
return 0;
}
编译结果:服务端相关代码在上一章节中,客户端write相关信息给服务端,服务端先read并打印调试信息再write发送给客户端中的read,客户端再将read调试信息打印出来。
实现客户端与服务端间信息交流
服务端
相关函数入下
?(1)socket函数
int socket(int domain,int type, int protocol);
(2)bind函数
int bind(int sockfd, const struct sockaddr *addr,int addrlen);
(3)listen函数
int listen(SOCKET sockfd, int backlog);
(4)accept函数
int accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen);
(5)read函数
ssize_t read(int fd,void *buf,size_t nbyte);
(6)write函数
ssize_t write(int fd, const void *buf,size_t nbytes);
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.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[128] ={0};
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 is not good\n");
exit(-1);
}
s_fd = socket(AF_INET,SOCK_STREAM,0);
if(s_fd == -1)
{
perror("socket");
exit(-1);
}
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(atoi(argv[2]));//将字符串转化为整型数
inet_aton(argv[1],&s_addr.sin_addr);
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
listen(s_fd,10);
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)/创建子进程
{
if(fork() == 0)
{
while(1)
{
memset(msg,0,sizeof(msg));//清空数据
printf("input:");
gets(msg);//scanf()
write(c_fd,msg,strlen(msg));
}
}
while(1)
{
n_read = read(c_fd,readBuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("get message:%d,%s\n",n_read,readBuf);
}
}
break;
}
}
return 0;
}
客户端
相关函数如下
?(1)socket函数
int socket(int domain,int type, int protocol);
(2)connect函数
int connect (int sockfd, struct sockaddr *addr, socklen_t addrlen);
(3)write函数
ssize_t write(int fd,void *buf,size_t nbyte);
(4)raad函数
ssize_t read(int fd, const void *buf,size_t nbytes);
代码示例
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.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};
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
if(argc != 3)
{
printf("parm is not good\n");
exit(-1);
}
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);
if(connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr)) == -1)
{
perror("connect");
exit(-1);
}
while(1)
{
if(fork() == 0)
{
while(1)
{
memset(msg,0,sizeof(msg));
printf("input: ");
gets(msg);
write(c_fd,msg,strlen(msg));
}
}
while(1)
{
memset(msg,0,sizeof(msg));
n_read = read(c_fd,readBuf,128);
if(n_read == -1)
{
perror("read");
}
else
{
printf("get message from FuwuDuan:%d,%s\n",n_read,readBuf);
}
}
}
return 0;
}
代码现象
实现了服务端与客户端信息交流
文章来源:https://blog.csdn.net/2301_78772787/article/details/135209830
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!