对Linux的网络编程比较熟悉,但是最近的项目需要在Windons上做应用程序,花了几天时间学了一点点!
这里先多谢百度文库给推荐的教程:http://wenku.baidu.com/course/view/19dcad51f01dc281e53af000?cid=5
大家可以去学习下!觉得非常不错!
服务器程序:
/************************************************************* * Copyright: (C) 2014 ieczw<ieczw@qq.com> * All rights reserved. * * Filename: soser.c * Description: This file * * Version: 1.0.0(02/19/2014~) * Author: ieczw <ieczw@qq.com> * ChangeLog: 1, Release initial version on "02/19/2014 02:51:54 PM" * **************************************************************/ #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") int main(int argc,char **argv) { //创建套接字 WORD myVersionRequest; WSADATA wsaData; myVersionRequest=MAKEWORD(1,1); int err; err=WSAStartup(myVersionRequest,&wsaData); if (!err){ printf("已打开套接字\n"); }else{ printf("ERROR:嵌套字未打开!"); return 1; } //进一步绑定套接字 SOCKET serSocket=socket(AF_INET,SOCK_STREAM,0);//创建了可识别套接字 //需要绑定的参数 SOCKADDR_IN addr; addr.sin_family=AF_INET; addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//ip地址 addr.sin_port=htons(6000);//绑定端口 bind(serSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR));//绑定完成 listen(serSocket,5);//其中第二个参数代表能够接收的最多的连接数 ////////////////////////////////////////////////////////////////////////// //开始进行监听 ////////////////////////////////////////////////////////////////////////// SOCKADDR_IN clientsocket; int len=sizeof(SOCKADDR); while (1) { SOCKET serConn=accept(serSocket,(SOCKADDR*)&clientsocket,&len);//如果这里不是accept而是conection的话。。就会不断的监听 char sendBuf[100]; sprintf(sendBuf,"hello, %s !",inet_ntoa(clientsocket.sin_addr));//找对对应的IP并且将这行字打印到那里 printf("Send:%s\n",sendBuf); send(serConn,sendBuf,strlen(sendBuf)+1,0); char receiveBuf[100];//接收 recv(serConn,receiveBuf,sizeof(receiveBuf),0); printf("recv:%s\n",receiveBuf); closesocket(serConn);//关闭 WSACleanup();//释放资源的操作 return 0; } return 1; }
客户端程序:
/********************************************************* **** * Copyright: (C) 2014 ieczw<ieczw@qq.com> * All rights reserved. * * Filename: client.c * Description: Socket Client * * Version: 1.0.0(02/19/2014~) * Author: ieczw <ieczw@qq.com> * ChangeLog: 1, Release initial version on "02/19/2014 02:51:54 PM" * ********************************************************** ****/ #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") int main(int argc,char **argv) { int err; WORD versionRequired; WSADATA wsaData; versionRequired=MAKEWORD(1,1); err=WSAStartup(versionRequired,&wsaData);//协议库的版本 信息 if (!err) { printf("客户端嵌套字已经打开!\n"); }else{ printf("ERROR:客户端的嵌套字打开失败!\n"); return 1;//结束 } SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN clientsock_in; clientsock_in.sin_addr.S_un.S_addr=inet_addr ("127.0.0.1"); clientsock_in.sin_family=AF_INET; clientsock_in.sin_port=htons(6000); //bind(clientSocket,(SOCKADDR*)&clientsock_in,strlen (SOCKADDR));//注意第三个参数 //listen(clientSocket,5); connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof (SOCKADDR));//开始连接 char receiveBuf[100]; if(true){ printf("Send:%s\n","hello,this is client"); send(clientSocket,"hello,this is client",strlen ("hello,this is client")+1,0); recv(clientSocket,receiveBuf,101,0); printf("Recv:%s\n",receiveBuf); closesocket(clientSocket); WSACleanup(); } return 0; }