网络编程 0905作业
作业
1、流式域套接字敲一遍。
服务器 server.c 代码
#include <myhead.h>
#define BACKLOG 10
int main(int argc, const char *argv[])
{
//1、创建流式域套接字
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);
if(oldfd == -1)
{
perror("socket");
return -1;
}
//2、判断文件存在存在就删除
if(access("./myserver",F_OK)==0)
{
if(unlink("./myserver")==-1)
{
perror("unlink");
return -1;
}
}
printf("删除文件成功\n");
//3、定义结构体信息并绑定
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;
}
printf("绑定成功\n");
//4、监听
if(listen(oldfd,BACKLOG)==-1)
{
perror("listen");
return -1;
}
printf("监听成功\n");
//5、接收客户端请求
int newfd = accept(oldfd,NULL,NULL);
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("接收客户端请求成功\n");
//6、数据收发
char buff[1024];
while(1)
{
int len = recv(newfd,buff,sizeof(buff),0);
if(len==0)
{
printf("客户端跑了\n");
break;
}
strcat(buff,"^_^");
send(newfd,buff,sizeof(buff),0);
printf("转发成功\n");
}
close(newfd);
close(oldfd);
return 0;
}
客户端 clinet.c 代码
#include <myhead.h>
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_UNIX,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、判断文件是否存在,存在就删除
if(access("./myclient",F_OK)==0)
{
if(unlink("./myclient")==-1)
{
perror("unlink");
return -1;
}
}
//3、绑定(可选)
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");//很重要,连接服务器的文件,而不是自己的文件
//4、连接服务器
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
printf("连接服务器成功\n");
//5、收发信息
char buff[1024];
while(1)
{
bzero(buff,sizeof(buff));
printf("请输入要发送的信息:");
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);//发送信息
printf("发送成功\n");
if(strcmp(buff,"quit")==0)
{
break;
}
recv(oldfd,buff,sizeof(buff),0);//接收服务器信息
printf("接收服务器的信息:%s\n",buff);//输出接收到的信息
}
close(oldfd);
return 0;
}
运行结果:
2.、UDP的客户端服务器实现一遍。
UDP服务器 UDPserver.c 代码
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.19.128"
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);//SOCK_DGRAM:UDP
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 buff[100];
while(1)
{
memset(buff,0,sizeof(buff));
//阻塞后就可以了
int len = recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,&cinlen);//不用管谁发的
if(len==0)
{
printf("客户端跑了\n");
break;
}
printf("服务器接收的信息:%s\n",buff);
}
close(oldfd);
return 0;
}
UDP客户端 clinet.c 代码
#include <myhead.h>
#define CLIPORT 6666
#define CLIIP "192.168.19.128"
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 cin = {
.sin_family = AF_INET,
.sin_port = htons(CLIPORT),
.sin_addr.s_addr = inet_addr(CLIIP)
};
char buff[1024];
while(1)
{
printf("客户端发消息:");
fgets(buff,sizeof(buff),stdin);
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,sizeof(cin));
printf("发送成功\n");
}
close(oldfd);
return 0;
}
运行结果: