简单的Socket通信代码

http://blog.csdn.net/hannosogno/article/details/6788276



1 #include <string> 2 #include<stdlib.h> 3 #include<stdio.h> 4 #include<conio.h> 5 #include<winsock2.h> 6 #include<windows.h> 7 #pragma comment(lib,"ws2_32.lib") 8 using namespace std; 9 10 void sbBroadcast(void) 11 { 12 SOCKET listener; 13 sockaddr_in listen_adress; 14 WSADATA wsdata; 15 BOOL optval; 16 //启动SOCKET库,版本为2.0 17 WSAStartup(0x0202,&wsdata); 18 optval=TRUE; 19 20 //然后赋值给两个地址,一个用来绑定套接字,一个用来从网络上的广播地址接收消息; 21 listen_adress.sin_family=AF_INET; 22 listen_adress.sin_addr.s_addr=INADDR_ANY; 23 listen_adress.sin_port=htons(9290); 24 listener=socket(AF_INET,SOCK_DGRAM, 0); 25 //设置该套接字为广播类型, 26 //setsockopt(listener,SOL_SOCKET,SO_BROADCAST,(char FAR *)&optval,sizeof(optval)); 27 bind(listener,(sockaddr *)&listen_adress,sizeof(sockaddr_in)); 28 listen(listener, 5); 29 30 char buf[256]; 31 while(1) 32 {//从广播地址接收消息,注意用来绑定的地址和接收消息的地址是不一样的 33 sockaddr_in from; 34 int fromlength = sizeof(SOCKADDR); 35 int rev = recvfrom(listener,buf, sizeof(buf),0,(struct sockaddr FAR *)&from,(int FAR *)&fromlength); 36 if (rev != SOCKET_ERROR) 37 { 38 static int num = 0; 39 printf("%s(%d) : %s\n",inet_ntoa(from.sin_addr), num, buf); //在此处输出检查到的IP,对吗???? 40 ZeroMemory(buf, sizeof(buf)); 41 num++; 42 } 43 } 44 } 45 46 void fbBroadcast(void) 47 { 48 SOCKET s; 49 sockaddr_in addrto; 50 WSADATA wsdata; 51 BOOL bsocket; 52 char *smsg="127.0.0.1&9290&255.255.255.255&9290&1&HelloMessage"; 53 WSAStartup(0x0202,&wsdata); 54 55 s=WSASocket(AF_INET,SOCK_DGRAM, IPPROTO_UDP,NULL,0,WSA_FLAG_OVERLAPPED); 56 bsocket=TRUE; 57 //然后赋值给地址,用来从网络上的广播地址接收消息; 58 addrto.sin_family=AF_INET; 59 addrto.sin_addr.s_addr=INADDR_BROADCAST; 60 addrto.sin_port=htons(9290); 61 bool opt=true; 62 //设置该套接字为广播类型, 63 setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char FAR *)&opt,sizeof(opt)); 64 int nlen=sizeof(addrto); 65 while(1) 66 { 67 Sleep(10); 68 static int num = 0; 69 //从广播地址发送消息 70 int ret=sendto(s,smsg,256,0,(sockaddr*)&addrto,nlen); 71 if(ret==SOCKET_ERROR) 72 { 73 printf("GetLastError: %d \n",WSAGetLastError()); 74 } 75 else 76 { 77 printf("ok, num = %d \n", num); 78 } 79 num++; 80 Sleep(1000); 81 } 82 } 83 84 void sbTcp(void) 85 { 86 SOCKET listener; 87 sockaddr_in listen_adress; 88 WSADATA wsdata; 89 //启动SOCKET库,版本为2.0 90 WSAStartup(0x0202,&wsdata); 91 92 //然后赋值给两个地址,一个用来绑定套接字,一个用来从网络上的广播地址接收消息; 93 listen_adress.sin_family=AF_INET; 94 listen_adress.sin_addr.s_addr=INADDR_ANY; 95 listen_adress.sin_port=htons(9290); 96 listener=socket(AF_INET,SOCK_STREAM, 0); 97 98 bind(listener,(sockaddr *)&listen_adress,sizeof(sockaddr_in)); 99 listen(listener, 5); 100 101 char buf[256]; 102 while(1) 103 {//从广播地址接收消息,注意用来绑定的地址和接收消息的地址是不一样的 104 sockaddr_in from; 105 int fromlength = sizeof(SOCKADDR); 106 SOCKET temp = accept(listener, (sockaddr *)&from, &fromlength); 107 108 recv(temp, buf, 256, 0); 109 printf("%s : %s\n",inet_ntoa(from.sin_addr), buf); //在此处输出检查到的IP,对吗???? 110 111 112 string msg = "ok~~~~~~~~~~"; 113 114 printf("preper to send\n"); 115 116 send(temp, msg.c_str(), msg.size() + 2, 0); 117 ZeroMemory(buf,256); 118 closesocket(temp); 119 } 120 } 121 122 123 void FbTcp(void) 124 { 125 sockaddr_in server_addres; 126 WSADATA wsdata; 127 char *smsg="Hello Socket!"; 128 WSAStartup(0x0202,&wsdata); 129 130 //然后赋值给地址,用来从网络上的广播地址接收消息; 131 server_addres.sin_family=AF_INET; 132 server_addres.sin_addr.s_addr = inet_addr("127.0.0.1"); 133 server_addres.sin_port=htons(9290); 134 135 int nlen=sizeof(server_addres); 136 while(1) 137 { 138 SOCKET s = socket(AF_INET,SOCK_STREAM, 0); 139 int ret = connect(s, (LPSOCKADDR)&server_addres, sizeof(server_addres)); 140 141 static int num = 0; 142 char buf[256]; 143 //从广播地址发送消息 144 ret=send(s,smsg,256,0); 145 ret = recv(s, buf, 256, 0); 146 printf("%s @ %l", buf, &buf); 147 if(ret==SOCKET_ERROR) 148 { 149 printf("GetLastError: %d \n",WSAGetLastError()); 150 } 151 else 152 { 153 printf("ok, num = %d \n", num); 154 } 155 num++; 156 closesocket(s); 157 158 Sleep(100); 159 } 160 } 161 162 163 164 #include <string> 165 #include <iostream> 166 using namespace std; 167 168 169 170 int main() 171 { 172 char c = getchar(); 173 if (c == 's') 174 { 175 sbBroadcast(); 176 } 177 else if(c == 'f') 178 { 179 fbBroadcast(); 180 } 181 else if (c == 'z') 182 { 183 sbTcp(); 184 } 185 else 186 { 187 FbTcp(); 188 } 189 }

 

posted @ 2013-01-08 22:22  rookieeeeee  阅读(3346)  评论(0编辑  收藏  举报