4月7日作业
作業1:tcp
?TCP-fuwuqi.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
?? ?fprintf(stderr,"line:%d", ?__LINE__);\
?? ?perror(msg);\
}while(0)
#define IP "192.168.64.135" ?//本機IP ifconfig查看
#define PORT 8888 ? //1024~49151
int main(int argc, const char *argv[])
{
?? ?//創建字節流套接字
?? ?int sfd=socket(AF_INET,SOCK_STREAM,0);
?? ?if(sfd<0)
?? ?{
?? ??? ?ERR_MSG("socket");
?? ??? ?return -1;
?? ?}
//?? ?printf("sfd=%d\n",sfd);//打印套接字的文件描述符
// 允許端口快速重用
?? ?int reuse=1;
?? ?if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
?? ?{
?? ??? ?ERR_MSG("setsockopt");
?? ??? ?return -1;
?? ?}
?? ?//填充地址信息結構體
?? ?//真實的地址信息結構體根據地址族指定 AF_INET:man 7 ip查看
?? ?struct sockaddr_in sin;
?? ?sin.sin_family=AF_INET;//必須填AF_INET
?? ?sin.sin_port=htons(PORT);//端口號:1024~49151
?? ?sin.sin_addr.s_addr=inet_addr(IP);//本機IP
?? ?//將ip和端口號綁定到套接字上
?? ?if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
?? ?{
?? ??? ?ERR_MSG("bind");
?? ??? ?return -1;
?? ?}
?? ?printf("bind success __%d__\n",__LINE__);
?? ?//將套接字設置為被動監聽狀態,監聽是否有客戶端連接成功
?? ?if(listen(sfd,64)<0)
?? ?{
?? ??? ?ERR_MSG("listen");
?? ??? ?return -1;
?? ?}
?? ?printf("listen success __%d__",__LINE__);
?? ?
?? ?struct sockaddr_in cin;//存儲連接成功的客戶端地址信息
?? ?socklen_t addrlen=sizeof(cin);
?? ?//阻塞函數,從已經完成連接的隊列頭中獲取一個客戶端信息,生成一個新的套接字
?? ?//該文件描述符才是與客戶端通信的文件描述符
?? ?int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
?? ?if(newfd<0)
?? ?{
?? ??? ?ERR_MSG("accept");
?? ??? ?return -1;
?? ?}
?? ?printf("newfd+%d,連接成功__%d__\n",newfd,__LINE__);
?? ?
?? ?char buf[128]="";
?? ?ssize_t res=0;
?? ?while(1)
?? ?{
?? ??? ?bzero(buf,sizeof(buf));//清空buf
?? ??? ?//接收客戶端信息
?? ??? ?res=recv(newfd,buf,sizeof(buf),0);
?? ??? ?if(res<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("recv");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?else if(0==res)
?? ??? ?{
?? ??? ??? ?printf("[%s:%d] newfd=%d ?客戶端下線__%d__\n",\
?? ??? ??? ??? ??? ?inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?printf("[%s:%d] ?newf=%d ? 所接收到的信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
?? ??? ?if(0==strcmp(buf,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?//發送 將數據拼接字符串后發送回去
?? ??? ?/*
?? ??? ?strcat(buf,"*!*");
?? ??? ?if(send(newfd,buf,sizeof(buf),0)<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ?*/?? ?/***********/
?? ??? ?//發送 將前端的消息發送給指定的客戶端
?? ??? ?char c[128]="";
?? ??? ?bzero(c,sizeof(c));
?? ??? ?printf("請輸入你要的發送的信息:");
?? ??? ?gets(c);
?? ??? ?if(send(newfd,c,sizeof(c),0)<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ??? ?if(0==strcmp(c,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?close(newfd);
?? ?close(sfd);
?? ?return 0;
}
TCP-kehduan.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
?? ?fprintf(stderr,"line:%d", ?__LINE__);\
?? ?perror(msg);\
}while(0)
#define IP "192.168.64.135" ?//本機IP ifconfig查看
#define PORT 8888 ? //1024~49151
int main(int argc, const char *argv[])
{
?? ?//創建字節流套接字
?? ?int cfd =socket(AF_INET,SOCK_STREAM,0);
?? ?if(cfd <0)
?? ?{
?? ??? ?ERR_MSG("socket");
?? ??? ?return -1;
?? ?}
//?? ?printf("cfd =%d\n",cfd );//打印套接字的文件描述符
// 允許端口快速重用 客戶端有沒有都一樣
?? ?int reuse=1;
?? ?if(setsockopt(cfd ,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
?? ?{
?? ??? ?ERR_MSG("setsockopt");
?? ??? ?return -1;
?? ?}
?? ?//填充地址信息結構體
?? ?//真實的地址信息結構體根據地址族指定 AF_INET:man 7 ip查看
?? ?struct sockaddr_in sin;
?? ?sin.sin_family=AF_INET;//必須填AF_INET
?? ?sin.sin_port=htons(PORT);//端口號:1024~49151
?? ?sin.sin_addr.s_addr=inet_addr(IP);//本機IP
?? ?//判定客戶端的地址信息結構體,非必須綁定
?? ?//如果不綁定,則有操作系統自動選擇一個端口號以及本機可用ip綁定到套接字上
?? ?
?? ?//連接服務器
?? ?if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
?? ?{
?? ??? ?ERR_MSG("connect");
?? ??? ?return -1;
?? ?}
?? ?printf("connect success __%d__\n",__LINE__);
?? ?
?? ?
?? ?char buf[128]="";
?? ?ssize_t res=0;
?? ?char c[128]="";
?? ?while(1)
?? ?{
?? ??? ?bzero(c,sizeof(c));//清空buf
?? ??? ?//發送 將數據拼接字符串后發送回去
?? ??? ?/*
?? ??? ?strcat(buf,"*!*");
?? ??? ?if(send(newfd,buf,sizeof(buf),0)<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ?*/?? ?/***********/
?? ??? ?//發送 將客戶端的消息發送給指定的服務器
?? ??? ?printf("請輸入你要的發送的信息:");
?? ??? ?gets(c);
?? ??? ?if(send(cfd,c,sizeof(c),0)<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ??? ?if(0==strcmp(c,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?//接收服務器端信息
?? ??? ?bzero(buf,sizeof(buf));//清空buf
?? ??? ?res=recv(cfd,buf,sizeof(buf),0);
?? ??? ?if(res<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("recv");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?else if(0==res)
?? ??? ?{
?? ??? ??? ?printf("cfd=%d ?客戶端下線__%d__\n",\
?? ??? ??? ??? ?cfd,__LINE__);
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?printf(" cfd=%d ? 所接收到的信息:%s\n",cfd,buf);
?? ??? ?if(0==strcmp(buf,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ?
?? ?}
?? ?
?? ?close(cfd );
?? ?return 0;
}
作業2:udp
DUP-fuwqi.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
?? ?fprintf(stderr,"line:%d", ?__LINE__);\
?? ?perror(msg);\
}while(0)
#define IP "192.168.64.135" ?//本機IP ifconfig查看
#define PORT 8888 ? //1024~49151
int main(int argc, const char *argv[])
{
?? ?//創建報式套接字
?? ?int sfd=socket(AF_INET,SOCK_DGRAM,0);
?? ?if(sfd<0)
?? ?{
?? ??? ?ERR_MSG("socket");
?? ??? ?return -1;
?? ?}
// 允許端口快速重用
?? ?int reuse=1;
?? ?if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
?? ?{
?? ??? ?ERR_MSG("setsockopt");
?? ??? ?return -1;
?? ?}
?? ?//填充服務器地址信息結構體
?? ?//真實的地址信息結構體根據地址族指定 AF_INET:man 7 ip查看
?? ?struct sockaddr_in sin;
?? ?sin.sin_family=AF_INET;//必須填AF_INET
?? ?sin.sin_port=htons(PORT);//端口號的網絡字節序:1024~49151
?? ?sin.sin_addr.s_addr=inet_addr(IP);//本機IP
?? ?//將ip和端口號綁定到套接字上
?? ?if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
?? ?{
?? ??? ?ERR_MSG("bind");
?? ??? ?return -1;
?? ?}
?? ?printf("bind success __%d__\n",__LINE__);
?? ?struct sockaddr_in cin;//存儲數據包是從哪里來的
?? ?socklen_t addrlen=sizeof(cin);
?? ?char buf[128]="";
?? ?ssize_t res=0;
?? ?char c[128]="";
?? ?while(1)
?? ?{
?? ??? ?bzero(buf,sizeof(buf));//清空buf
?? ??? ?//接收數據,必須接收數據包的發送方地址信息給sendto使用
?? ??? ?res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
?? ??? ?if(res<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("recvfrom");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("[%s:%d] ? ? 所接收到的信息:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
?? ??? ?if(0==strcmp(buf,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?//發送 誰發給我,我就發給誰
?? ??? ?bzero(c,sizeof(c));
?? ??? ?printf("請輸入你要的發送的信息:");
?? ??? ?gets(c);
?? ??? ?if(sendto(sfd,c,sizeof(c),0,(struct sockaddr*)&cin,sizeof(cin))<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ??? ?if(0==strcmp(c,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?close(sfd);
?? ?return 0;
}
UDP-kehuduan.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
?? ?fprintf(stderr,"line:%d", ?__LINE__);\
?? ?perror(msg);\
}while(0)
#define IP "192.168.64.135" ?//本機IP ifconfig查看
#define PORT 8888 ? //1024~49151
int main(int argc, const char *argv[])
{
?? ?//創建報式套接字
?? ?int sfd=socket(AF_INET,SOCK_DGRAM,0);
?? ?if(sfd<0)
?? ?{
?? ??? ?ERR_MSG("socket");
?? ??? ?return -1;
?? ?}
?? ?//非必須綁定 如果不綁定則由操作系統自動綁定
?? ?//填充服務器地址信息結構體
?? ?//真實的地址信息結構體根據地址族指定 AF_INET:man 7 ip查看
?? ?struct sockaddr_in cin;
?? ?cin.sin_family=AF_INET;//必須填AF_INET
?? ?cin.sin_port=htons(PORT);//端口號的網絡字節序:1024~49151
?? ?cin.sin_addr.s_addr=inet_addr(IP);//本機IP
?? ?struct sockaddr_in rcvaddr;
?? ?socklen_t addrlen=sizeof(rcvaddr);
?? ?char buf[128]="";
?? ?ssize_t res=0;
?? ?char c[128]="";
?? ?while(1)
?? ?{
?? ??? ?//發送 發送給指定服務器
?? ??? ?bzero(c,sizeof(c));
?? ??? ?printf("請輸入你要的發送的信息:");
?? ??? ?gets(c);
?? ??? ?if(sendto(sfd,c,sizeof(c),0,(struct sockaddr*)&cin,sizeof(cin))<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("send");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("發送成功\n");
?? ??? ?if(0==strcmp(c,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?bzero(buf,sizeof(buf));//清空buf
?? ??? ?//接收數據,必須接收數據包的發送方地址信息給sendto使用
?? ??? ?res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&addrlen);
?? ??? ?if(res<0)
?? ??? ?{
?? ??? ??? ?ERR_MSG("recvfrom");
?? ??? ??? ?return -1;
?? ??? ?}
?? ??? ?printf("[%s:%d] ? ? 所接收到的信息:%s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
?? ??? ?if(0==strcmp(buf,"quit"))//如果輸入quit自動退出
?? ??? ?{
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?close(sfd);
?? ?return 0;
}
?
?
總結
- 上一篇: 数据库(三范式,视图,事务隔离级别,存储
- 下一篇: 讯飞错误码10116