#include <stdio.h>
#include <winsock2.h>
void main()
{
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( m_socket == INVALID_SOCKET )
{
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}
// Bind the socket.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
service.sin_port = htons( 27015 );
if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
printf( "bind() failed.\n" );
closesocket(m_socket);
return;
}
// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");
// Accept connections.
SOCKET AcceptSocket;
printf( "Waiting for a client to connect...\n" );
while (1)
{
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR )
{
AcceptSocket = accept( m_socket, NULL, NULL );
}
printf( "Client Connected.\n");
m_socket = AcceptSocket;
break;
}
// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "조산 서버";
char recvbuf[32] = "";
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
printf( "Bytes Recv: %ld\n", bytesRecv );
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );
printf("Recv Buf: %s\n", recvbuf);
return;
}
'NativeCode > api' 카테고리의 다른 글
C++ Style Socket (0) | 2010.03.19 |
---|---|
Client Source (0) | 2010.03.19 |
winsock 자신의 아이피 알아내기 (0) | 2010.03.19 |
Winsock 상대방 아이피, 포트 알아내기 (0) | 2010.03.19 |
IPHLPAPI(IP Helper API) functions (0) | 2010.03.19 |