您的位置:首页 > 财经 > 金融 > 网络编程9.5

网络编程9.5

2025/4/4 13:47:43 来源:https://blog.csdn.net/jay__jay_/article/details/141940139  浏览:    关键词:网络编程9.5

流式域客户端:

#include <myhead.h>
int main(int argc, const char *argv[])
{int oldfd = socket(AF_UNIX,SOCK_STREAM,0);if(oldfd==-1){perror("socket");return -1;}if(access("./myclient",F_OK)==0){if(unlink("./myclient")==-1){perror("unlink");return -1;}}struct sockaddr_un client;client.sun_family = AF_UNIX;strcpy(client.sun_path,"./myclient");if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1){perror("bind");return -1;}struct sockaddr_un server;server.sun_family = AF_UNIX;strcpy(server.sun_path,"./myserver");if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1){perror("connect");return -1;}printf("连接服务器成功\n");char buf[1024];while(1){printf("请输入要发送的消息:");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = '\0';send(oldfd,buf,sizeof(buf),0);printf("发送成功\n");if(strcmp(buf,"quit")==0){break;}recv(oldfd,buf,sizeof(buf),0);printf("接收服务器的信息:%s\n",buf);}return 0;
}

流式域服务器:

#include <myhead.h>
#define BACKLOG 10
int main(int argc, const char *argv[])
{int oldfd = socket(AF_UNIX,SOCK_STREAM,0);if(oldfd==-1){perror("socket");return -1;}if(access("./myserver",F_OK)==0){if(unlink("./myserver")==-1){perror("unlink");return -1;}}struct sockaddr_un server;server.sun_family = AF_UNIX;strcpy(server.sun_path,"./myserver");if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1){perror("bind");return -1;}if(listen(oldfd,BACKLOG)==-1){perror("listen");return -1;}int newfd = accept(oldfd,NULL,NULL);if(newfd==-1){perror("accept");return -1;}char buf[1024];while(1){int len = recv(newfd,buf,sizeof(buf),0);if(len==0){printf("客户端跑了\n");break;}strcat(buf,"^_^");send(newfd,buf,sizeof(buf),0);printf("转发成功\n");}close(newfd);close(oldfd);return 0;
}

UDP客户端:

#include <myhead.h>
#define CLIPORT 9999
#define CLIIP "192.168.0.115"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_DGRAM,0);if(oldfd==-1){perror("socket");return -1;}struct sockaddr_in sin = {.sin_family = AF_INET,.sin_port = htons(CLIPORT),.sin_addr.s_addr = inet_addr(CLIIP)};char buf[1024];while(1){printf("客户端请输入消息:");fgets(buf,sizeof(buf),stdin);sendto(oldfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,sizeof(sin));printf("发送成功\n");}close(oldfd);return 0;
}

UDP服务器:

#include <myhead.h>
#define SERPORT 9999
#define SERIP "192.168.0.115"
int main(int argc, const char *argv[])
{//1、创建套接字int oldfd = socket(AF_INET,SOCK_DGRAM,0);if(oldfd==-1){perror("socket");return -1;}//2、绑定struct sockaddr_in sin = {.sin_family = AF_INET,.sin_port = htons(SERPORT),.sin_addr.s_addr = inet_addr(SERIP)};if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1){perror("bind");return -1;}//3、收发消息struct sockaddr_in cin;socklen_t cinlen = sizeof(cin);char buf[1024];while(1){memset(buf,0,sizeof(buf));int len = recvfrom(oldfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&cinlen);if(len==0){printf("客户端跑了\n");break;}printf("服务器收到的消息:%s\n",buf);}close(oldfd);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com