윈도우즈에서 socket

7601 단어 windows
     ,   。。。      ,     。。。。。。。

//TCP
//      
#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 BACKLOG 10
#define TRUE 1

void main( void )
{
int iServerSock;
int iClientSock;

char *buf = "hello, world!
"; struct sockaddr_in ServerAddr; struct sockaddr_in ClientAddr; int sin_size; WSADATA WSAData; if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )// { printf( "initializationing error!
" ); WSACleanup( ); exit( 0 ); } if( ( iServerSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET ) { printf( " !
" ); WSACleanup( ); exit( 0 ); } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons( PORT );// ServerAddr.sin_addr.s_addr = INADDR_ANY;// IP memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) ); if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 ) { printf( "bind !
" ); WSACleanup( ); exit( 0 ); } if( listen( iServerSock, BACKLOG ) == -1 ) { printf( "listen !
" ); WSACleanup( ); exit( 0 ); } while( TRUE ) { sin_size = sizeof( struct sockaddr_in ); iClientSock = accept( iServerSock, ( struct sockaddr * )&ClientAddr, &sin_size ); if( iClientSock == -1 ) { printf( "accept !
" ); WSACleanup( ); exit( 0 ); } printf( " %s
", inet_ntoa( ClientAddr.sin_addr ) ); if( send( iClientSock, buf, strlen( buf ), 0 ) == -1 ) { printf( "send !" ); closesocket( iClientSock ); WSACleanup( ); exit( 0 ); } } } ///// #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 BACKLOG 10 #define TRUE 1 #define MAXDATASIZE 100 void main( void ) { int iClientSock; char buf[ MAXDATASIZE ]; struct sockaddr_in ServerAddr; int numbytes; // struct hostent *he; WSADATA WSAData; // int sin_size; /* if( ( he = gethostbyname( "liuys" ) ) == NULL ) { printf( "gethostbyname !" ); WSACleanup( ); exit( 0 ); } */ if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) )// { printf( "initializationing error!
" ); WSACleanup( ); exit( 0 ); } if( ( iClientSock = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET ) { printf( " !
" ); WSACleanup( ); exit( 0 ); } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons( PORT ); // ServerAddr.sin_addr = *( ( struct in_addr * )he->h_addr ); ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );// IP memset( &( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) ); if( connect( iClientSock, ( struct sockaddr * ) & ServerAddr, sizeof( struct sockaddr ) ) == -1 ) { printf( "connect !" ); WSACleanup( ); exit( 0 ); } numbytes = recv( iClientSock, buf, MAXDATASIZE, 0 ); if( numbytes == -1 ) { printf( "recv !" ); WSACleanup( ); exit( 0 ); } buf[ numbytes ] = '\0'; printf( "Received: %s", buf ); closesocket( iClientSock ); WSACleanup( ); } /////UDP // #include< stdio.h > #include< string.h > #include< winsock.h > #include< windows.h > #pragma comment( lib, "ws2_32.lib" ) #define PORT 2046 #define BACKLOG 10 #define TRUE 1 #define MAXDATASIZE 1000 void main( void ) { int iServerSock; // int iClientSock; int addr_len; int numbytes; char buf[ MAXDATASIZE ]; struct sockaddr_in ServerAddr; struct sockaddr_in ClientAddr; WSADATA WSAData; if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) ) { printf( "initializationing error!
" ); WSACleanup( ); exit( 0 ); } iServerSock = socket( AF_INET, SOCK_DGRAM, 0 ); if( iServerSock == INVALID_SOCKET ) { printf( " !
" ); WSACleanup( ); exit( 0 ); } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons( PORT );// ServerAddr.sin_addr.s_addr = INADDR_ANY;// IP memset( & ( ServerAddr.sin_zero ), 0, sizeof( ServerAddr.sin_zero ) ); if( bind( iServerSock, ( struct sockaddr * )&ServerAddr, sizeof( struct sockaddr ) ) == -1 ) { printf( "bind !
" ); WSACleanup( ); exit( 0 ); } addr_len = sizeof( struct sockaddr ); numbytes = recvfrom( iServerSock, buf, MAXDATASIZE, 0, ( struct sockaddr * ) & ClientAddr, &addr_len ); if( numbytes == -1 ) { printf( "recvfrom !
" ); WSACleanup( ); exit( 0 ); } printf( "got packet from %s
", inet_ntoa( ClientAddr.sin_addr ) ); printf( "packet is %d bytes long
", numbytes ); buf[ numbytes ] = '\0'; printf( "packet contains \"%s\"
", buf ); closesocket( iServerSock ); WSACleanup( ); } // #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 ] = { 0 }; WSADATA WSAData; if( WSAStartup( MAKEWORD( 1, 1 ), &WSAData ) ) { printf( "initializationing error!
" ); WSACleanup( ); exit( 0 ); } if( ( iClientSock = socket( AF_INET, SOCK_DGRAM, 0 ) ) == -1 ) { printf( " !
" ); WSACleanup( ); exit( 0 ); } ServerAddr.sin_family = AF_INET; ServerAddr.sin_port = htons( PORT ); ServerAddr.sin_addr.s_addr = inet_addr( "192.168.2.194" );// 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 !
" ); WSACleanup( ); exit( 0 ); } printf( "sent %d bytes to %s
", numbytes, inet_ntoa( ServerAddr.sin_addr ) ); closesocket( iClientSock ); WSACleanup( ); }

좋은 웹페이지 즐겨찾기