반응형
gcc -o server server.c -lws2_32
#include <winsock.h>
#include <stdio.h>
#define MAX_PACKETLEN 512
#define PORT 5115
int main()
{
WSADATA wsaData;
int status;
int SockLen;
int Readn,Writen;
SOCKET EndpointSocket, ClientSocket;
struct sockaddr_in SockInfo, ClientSockInfo;
char ReadBuffer[MAX_PACKETLEN];
if(WSAStartup(MAKEWORD(2,2),&wsaData)!= 0)
{
printf("error\r\n");
return 0;
}
EndpointSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( EndpointSocket == INVALID_SOCKET )
return 1;
printf("Success socket create\r\n");
ZeroMemory(&SockInfo, sizeof( struct sockaddr_in ));
SockInfo.sin_family = AF_INET;
SockInfo.sin_port = htons( PORT );
SockInfo.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
status = bind( EndpointSocket, (struct sockaddr*)&SockInfo, sizeof( struct sockaddr_in) );
if( status == SOCKET_ERROR)
{
printf("Bind Error\n");
return 0;
}
if( SOCKET_ERROR == listen( EndpointSocket, 5 ))
{
printf("listen Error\n");
return 0;
}
while(1)
{
ZeroMemory( &ClientSockInfo, sizeof( struct sockaddr_in ) );
SockLen = sizeof(struct sockaddr_in);
ClientSocket = accept( EndpointSocket, (struct sockaddr*)&ClientSockInfo, &SockLen );
if(ClientSocket == INVALID_SOCKET)
{
printf("Accept Error\n");
closesocket(EndpointSocket);
WSACleanup();
return 1;
}
printf("Accept Client\n");
Readn = recv( ClientSocket, ReadBuffer, MAX_PACKETLEN,0 );
if( Readn > 0 )
{
Writen = send( ClientSocket, ReadBuffer, Readn, 0 );
}
else
{
printf("read Error\n");
}
closesocket(ClientSocket);
}
closesocket( EndpointSocket );
WSACleanup();
return 0;
}
반응형
'BackEnd FrontEnd > C , C++' 카테고리의 다른 글
memory leak 확인하기 (0) | 2020.07.21 |
---|---|
mac에서 Jsoncpp 설치하기 (0) | 2020.01.23 |
linux C/C++ openssl 설치 후 빌드에러 (0) | 2020.01.22 |
[networkprogramming] mac에서 bind코드가 잘 안될때 (0) | 2020.01.21 |
linux C/C++ mysql 사용하기 (0) | 2020.01.21 |