winsock基础编程

[cpp]  view plain copy
  1. //TCP  
  2. //服务器端程序  
  3. #include< stdio.h >  
  4. #include< stdlib.h >  
  5. #include< windows.h >  
  6. #include< winsock.h >  
  7. #include< string.h >  
  8.   
  9. #pragma comment( lib, "ws2_32.lib" )  
  10.   
  11. #define PORT 2046  
  12. #define BACKLOG 10  
  13. #define TRUE 1  
  14.   
  15. void main( void )  
  16. {  
  17. int iServerSock;  
  18. int iClientSock;  
  19.   
  20. char *buf = "hello, world!\n";  
  21.   
  22. struct sockaddr_in ServerAddr;  
  23. struct sockaddr_in ClientAddr;  
  24.   
  25. int sin_size;  
  26.   
  27. WSADATA WSAData;  
  28.   
  29. if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化  
  30. {  
  31. printf( "initializationing error!\n" );  
  32. WSACleanup( );  
  33. exit( 0 );  
  34. }  
  35.   
  36. if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )  
  37. {  
  38. printf( "创建套接字失败!\n" );  
  39. WSACleanup( );  
  40. exit( 0 );  
  41. }  
  42.   
  43. ServerAddr.sin_family = AF_INET;  
  44. ServerAddr.sin_port = htons( PORT );//监视的端口号  
  45. ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP  
  46. memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );  
  47.   
  48.   
  49. if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeofstruct sockaddr ) ) == -1 )  
  50. {  
  51. printf( "bind调用失败!\n" );  
  52. WSACleanup( );  
  53. exit( 0 );  
  54. }  
  55.   
  56. if( listen( iServerSock, BACKLOG ) == -1 )  
  57. {  
  58. printf( "listen调用失败!\n" );  
  59. WSACleanup( );  
  60. exit( 0 );  
  61. }  
  62.   
  63. while( TRUE )  
  64. {  
  65. sin_size = sizeofstruct sockaddr_in );  
  66. iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size );  
  67.   
  68. if( iClientSock == -1 )  
  69. {  
  70. printf( "accept调用失败!\n" );  
  71. WSACleanup( );  
  72. exit( 0 );  
  73. }  
  74.   
  75. printf( "服务器连接到%s\n", inet_ntoa( ClientAddr.sin_addr ) );  
  76. if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 )  
  77. {  
  78. printf( "send调用失败!" );  
  79. closesocket( iClientSock );  
  80. WSACleanup( );  
  81. exit( 0 );  
  82. }  
  83. }  
  84. }  
  85.   
  86.   
  87. /客户端程序  
  88. #include< stdio.h >  
  89. #include< stdlib.h >  
  90. #include< windows.h >  
  91. #include< winsock.h >  
  92. #include< string.h >  
  93.   
  94. #pragma comment( lib, "ws2_32.lib" )  
  95.   
  96. #define PORT 2046  
  97. #define BACKLOG 10  
  98. #define TRUE 1  
  99. #define MAXDATASIZE 100  
  100.   
  101.   
  102. void main( void )  
  103. {  
  104.   
  105. int iClientSock;  
  106. char buf[ MAXDATASIZE ];  
  107. struct sockaddr_in ServerAddr;  
  108. int numbytes;  
  109. // struct hostent *he;  
  110. WSADATA WSAData;  
  111.   
  112. // int sin_size;  
  113.   
  114. /* if( ( he = gethostbyname( "liuys" ) ) == NULL ) 
  115. { 
  116. printf( "gethostbyname调用失败!" ); 
  117. WSACleanup( ); 
  118. exit( 0 ); 
  119. } 
  120. */  
  121.   
  122.   
  123. if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )//初始化  
  124. {  
  125. printf( "initializationing error!\n" );  
  126. WSACleanup( );  
  127. exit( 0 );  
  128. }  
  129.   
  130. if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET )  
  131. {  
  132. printf( "创建套接字失败!\n" );  
  133. WSACleanup( );  
  134. exit( 0 );  
  135. }  
  136.   
  137. ServerAddr.sin_family = AF_INET;  
  138. ServerAddr.sin_port = htons( PORT );  
  139. // ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr );  
  140. ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//记得换IP  
  141. memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );  
  142.   
  143. if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeofstruct sockaddr ) ) == -1 )  
  144. {  
  145. printf( "connect失败!" );  
  146. WSACleanup( );  
  147. exit( 0 );  
  148. }  
  149.   
  150. numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 );  
  151.   
  152. if( numbytes == -1 )  
  153. {  
  154. printf( "recv失败!" );  
  155. WSACleanup( );  
  156. exit( 0 );  
  157. }  
  158.   
  159. buf[ numbytes ] = '\0';  
  160.   
  161. printf( "Received: %s", buf );  
  162.   
  163. closesocket( iClientSock );  
  164. WSACleanup( );  
  165. }  
  166.   
  167.   
  168. /UDP  
  169. //服务器  
  170. #include< stdio.h >  
  171. #include< string.h >  
  172. #include< winsock.h >  
  173. #include< windows.h >  
  174.   
  175. #pragma comment( lib, "ws2_32.lib" )  
  176.   
  177. #define PORT 2046  
  178. #define BACKLOG 10  
  179. #define TRUE 1  
  180. #define MAXDATASIZE 1000  
  181.   
  182. void main( void )  
  183. {  
  184. int iServerSock;  
  185. // int iClientSock;  
  186.   
  187. int addr_len;  
  188. int numbytes;  
  189.   
  190. char buf[ MAXDATASIZE ];  
  191.   
  192. struct sockaddr_in ServerAddr;  
  193. struct sockaddr_in ClientAddr;  
  194.   
  195. WSADATA WSAData;  
  196. if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )  
  197. {  
  198. printf( "initializationing error!\n" );  
  199. WSACleanup( );  
  200. exit( 0 );  
  201. }  
  202.   
  203. iServerSock = socket( AF_INET, SOCK_DGRAM, 0 );  
  204. if( iServerSock == INVALID_SOCKET )  
  205. {  
  206. printf( "创建套接字失败!\n" );  
  207. WSACleanup( );  
  208. exit( 0 );  
  209. }  
  210.   
  211.   
  212. ServerAddr.sin_family = AF_INET;  
  213. ServerAddr.sin_port = htons( PORT );//监视的端口号  
  214. ServerAddr.sin_addr.s_addr = INADDR_ANY;//本地IP  
  215. memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );  
  216.   
  217.   
  218. if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeofstruct sockaddr ) ) == -1 )  
  219. {  
  220. printf( "bind调用失败!\n" );  
  221. WSACleanup( );  
  222. exit( 0 );  
  223. }  
  224.   
  225.   
  226. addr_len = sizeofstruct sockaddr );  
  227. numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len );  
  228. if( numbytes == -1 )  
  229. {  
  230. printf( "recvfrom调用失败!\n" );  
  231. WSACleanup( );  
  232. exit( 0 );  
  233. }  
  234.   
  235. printf( "got packet from %s\n", inet_ntoa( ClientAddr.sin_addr ) );  
  236. printf( "packet is %d bytes long\n", numbytes );  
  237. buf[ numbytes ] = '\0';  
  238. printf( "packet contains \"%s\"\n", buf );  
  239.   
  240. closesocket( iServerSock );  
  241. WSACleanup( );  
  242.   
  243.   
  244. }  
  245. //客户端  
  246. #include< stdio.h >  
  247. #include< stdlib.h >  
  248. #include< windows.h >  
  249. #include< winsock.h >  
  250. #include< string.h >  
  251.   
  252. #pragma comment( lib, "ws2_32.lib" )  
  253.   
  254. #define PORT 2046  
  255. #define MAXDATASIZE 100  
  256.   
  257.   
  258. void main( void )  
  259. {  
  260.   
  261. int iClientSock;  
  262. struct sockaddr_in ServerAddr;  
  263.   
  264. int numbytes;  
  265. char buf[ MAXDATASIZE ] = { 0 };  
  266.   
  267. WSADATA WSAData;  
  268. if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )  
  269. {  
  270. printf( "initializationing error!\n" );  
  271. WSACleanup( );  
  272. exit( 0 );  
  273. }  
  274.   
  275. if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )  
  276. {  
  277. printf( "创建套接字失败!\n" );  
  278. WSACleanup( );  
  279. exit( 0 );  
  280. }  
  281.   
  282. ServerAddr.sin_family = AF_INET;  
  283. ServerAddr.sin_port = htons( PORT );  
  284. ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );//记得换IP  
  285. memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );  
  286.   
  287.   
  288. numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeofstruct sockaddr ) );  
  289. if( numbytes == -1 )  
  290. {  
  291. printf( "sendto调用失败!\n" );  
  292. WSACleanup( );  
  293. exit( 0 );  
  294. }  
  295.   
  296. printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );  
  297.   
  298. closesocket( iClientSock );  
  299. WSACleanup( );  
  300. }   

转载自: http://space.itpub.net/13771794/viewspace-580550

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值