天天看點

簡單的網絡管理代碼

#include< stdio.h >

#include< stdlib.h >

#include< windows.h >

#include< winsock.h >

#include< string.h >

#pragma comment( lib, "ws2_32.lib" )

#define PORT 2046

#define MAXDATASIZE 100

void main( void )

{

int iClientSock;

struct sockaddr_in ServerAddr;

int numbytes;

char buf[ MAXDATASIZE ] = { "y" };

WSADATA WSAData;

if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )

printf( "initializationing error!\n" );

WSACleanup( );

exit( 0 );

}

if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 )

printf( "建立套接字失敗!\n" );

ServerAddr.sin_family = AF_INET;

ServerAddr.sin_port = htons( PORT );

ServerAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );//記得換IP

memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) );

numbytes = sendto( iClientSock, buf, strlen( buf ), 0, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) );

if( numbytes == -1 )

printf( "sendto調用失敗!\n" );

printf( "sent %d bytes to %s\n", numbytes, inet_ntoa( ServerAddr.sin_addr ) );

closesocket( iClientSock );

本文轉自 帥楓小明 51CTO部落格,原文連結:http://blog.51cto.com/576642026/318064,如需轉載請自行聯系原作者

繼續閱讀