欢迎来到天天文库
浏览记录
ID:42653682
大小:70.50 KB
页数:6页
时间:2019-09-19
《用c语言写socket套接字实现网络连接》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、计算机通信网第五次作业题目:用c语言写socket套接字实现网络连接运行结果附上源代码:Server源代码:#include#include#include#include#pragmacomment(lib,"ws2_32")usingnamespacestd;intmain(intargc,char*argv[]){WSADATAwasData;inti;stringstr;WSAStartup(MAKEWORD(2,2),&wasData);//
2、WSAStartupSOCKETs=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);//socketsockaddr_insockaddr;sockaddr.sin_family=PF_INET;sockaddr.sin_addr.S_un.S_addr=inet_addr("115.155.60.110");sockaddr.sin_port=htons(1200);bind(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR));//bindlisten(s,1);/
3、/listenSOCKADDRclientAddr;intnSize=sizeof(SOCKADDR);SOCKETclientSock;printf("正在连接...");clientSock=accept(s,(SOCKADDR*)&clientAddr,&nSize);//acceptprintf("连接成功");getchar();closesocket(clientSock);closesocket(s);//closesocketWSACleanup();//WSACleanupreturn(0);}Clie
4、nt源代码:#include#include#pragmacomment(lib,"ws2_32")intmain(){WSADATAwsaData;WSAStartup(MAKEWORD(2,2),&wsaData);SOCKETs=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);sockaddr_insockaddr;sockaddr.sin_family=PF_INET;sockaddr.sin_addr.S_un.S_addr=inet_addr(
5、"115.155.60.110");sockaddr.sin_port=htons(1200);connect(s,(SOCKADDR*)&sockaddr,sizeof(SOCKADDR));getchar();closesocket(s);WSACleanup();return(0);}
此文档下载收益归作者所有