PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!
TCP_server:
#include#pragma comment (lib, "ws2_32")//显式连接套接字库 int main(int argc, char *argv) { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData);//初始化套接字库 SOCKET s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in socketaddr; socketaddr.sin_family = PF_INET; socketaddr.sin_addr.S_un.S_addr = inet_addr("127.1.1.0"); socketaddr.sin_port = htons(827); bind(s,(SOCKADDR*)&socketaddr,sizeof(SOCKADDR)); listen(s,1); SOCKADDR clientAddr; int nSize = sizeof(SOCKADDR); SOCKET clientSock; clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize); send(clientSock,"hello client \r\n",strlen("hello client \r\n")+sizeof(char),NULL); closesocket(clientSock); closesocket(s); WSACleanup();//释放套接字库 return 0; }
TCP_client:
#include#include #pragma comment (lib,"ws2_32")//显示连接套接字库 int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData);//初始化套接字库 SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);//创立TCP套接字 sockaddr_in socketAddr; socketAddr.sin_family = PF_INET; socketAddr.sin_addr.S_un.S_addr = inet_addr("127.1.1.0"); socketAddr.sin_port = htons(827); connect(s,(SOCKADDR*)&socketAddr,sizeof(SOCKADDR)); char szBuffer[MAXBYTE] = {0}; recv(s,szBuffer,MAXBYTE,NULL); printf("szBuffer = %s \r\n",szBuffer); closesocket(s); WSACleanup();//释放套接字库 system("pause"); return 0; }
UDP_server:
每日一道理 翻开早已发黄的页张,试着寻找过去所留下的点点滴滴的足迹。多年前的好友似乎现在看来已变得陌生,匆忙之间,让这维持了多年的友谊变淡,找不出什么亲切感,只是偶尔遇上,淡淡地微笑,如今也只能在这发黄的页张中找寻过去的那些让人难忘的,至少我可以握住这仅剩下一段的“丝线头”……
#include#include #pragma comment(lib, "ws2_32.lib")int main(){ WSADATA wsData; SOCKET server; SOCKADDR_IN sockAddr; int sClitAddLen; SOCKET sClient; SOCKADDR_IN sClientAddr; int tmp; char buffer[512]; WSAStartup(MAKEWORD(2,2), &wsData); //初始化winsock server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创立winsock /*配置winsock*/ sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sockAddr.sin_port = htons(5503); memset(&(sockAddr.sin_zero), 0, sizeof(sockAddr.sin_zero)); /*将winsock绑定到端口*/ if(bind(server, (SOCKADDR *)&sockAddr, sizeof(SOCKADDR)) != 0) { printf("bind is failure\n"); return 0; } listen(server, 10); /*在该端口监听*/ while(1) { memset(buffer, 0, sizeof(buffer)); sClitAddLen = sizeof(SOCKADDR); /*响应客户端服务*/ sClient = accept(server, (SOCKADDR *)&sClientAddr, &sClitAddLen); if(sClient == INVALID_SOCKET) { printf("accept is failure\n"); } else { printf("Server is connect with a Client\n"); if(recv(sClient, buffer, 512, 0) == SOCKET_ERROR) { printf("recv failed!\n"); } else { printf("%s\n", buffer); } } closesocket(sClient); } closesocket(server); WSACleanup(); return 0;}
UDP_client:
#define _CRT_SECURE_NO_DEPRECATE //屏蔽scanf的正告#include#include #pragma comment(lib, "ws2_32.lib")int main(){ int tmp; WSADATA wsData; SOCKET client; SOCKADDR_IN sockAddr; char buffer[512]; WSAStartup(MAKEWORD(2,2), &wsData); //初始化winsock client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创立winsock /*配置winsock*/ sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(5503); memset(&(sockAddr.sin_zero), 0, sizeof(sockAddr.sin_zero)); /*连接到服务端*/ if(connect(client, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) { printf("connet is failure \n"); return 0; } else { printf("connet with the Server \n"); } scanf("%s",buffer,255); send(client, buffer, (int)strlen(buffer), 0); /*向服务端发送数据*/ /*关闭socket并释放其占用的资源*/ closesocket(client); WSACleanup(); return 0;}
文章结束给大家分享下程序员的一些笑话语录: 面试官:熟悉哪种语言
应聘者:JAVA 面试官:知道什么叫类么 应聘者:我这人实在,工作努力,不知道什么叫累 面试官:知道什么是包? 应聘者:我这人实在 平常不带包 也不用公司准备了 面试官:知道什么是接口吗? 应聘者:我这个人工作认真。从来不找借口偷懒 面试官:知道什么是继承么 应聘者:我是孤儿没什么可以继承的 面试官:知道什么叫对象么? 应聘者:知道,不过我工作努力,上进心强,暂时还没有打算找对象。 面试官:知道多态么? 应聘者:知道,我很保守的。我认为让心爱的女人为了自已一时的快乐去堕胎是不道德的行为!请问这和C#有什么关系??--------------------------------- 原创文章 By
client和api---------------------------------