You are on page 1of 2

II UDPServer Defines the entry point for the console application .

.cpp
II

#include "stdafx .h"


#include "stdio.h"
#include "conio .h"
#include "winsock2 .h"
#include "ws2tcpip .h"
#define SERVER PORT 5500
#define SERVER ADDR "127.0.0.1"
#define BUFF SIZE 2048

int _tmain(int argc, TCHAR* argv[ ])

//Step 1: Inittiate WinSock


WSADATA wsaData ;
WORD wVers ion = MAKEWORD( 2,2);
if (WSAStar tup(wVersion, &wsaData))
printf("Vers ion is not supported\n
");

//Step 2: Construct socket


SOCKET server;
server = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

//Step 3: Bind address to socket


sockaddr_in serverAddr;
serverAddr.sin_ family = AF_INET;
serverAddr.s in_port = htons(SERVER_PORT);
serverAddr .sin_addr .s_addr = inet_addr(SERVER_A DDR);
if(bind(server, (sockaddr *)&serverAddr, sizeof (serverAddr) ))
{
printf ("Error! Cannot bind this address.");
getch();
return O;

printf("Server

started! ");

//Step 4 : Communicate with client


sockaddr_in clientAddr;
char buff[BUFF_SIZE];
int ret, clientAddrLen sizeof(clientAddr);

while (1){
//Receive message
ret = recvfrom(server, buff, BUFF_SIZE, 0, (sockaddr *) &clientAddr,&clientAddrLen);
if(ret == SOCKET_ERROR)
printf ("Error : %", WSAGetLastError(
)); else if (strlen(buff) > 0){
buff [ret] = O;
printf ("Receive from client [%s:%d ] %s\n",
inet_ntoa(clientAddr .sin_addr) , ntohs(clientAddr .sin_port),buff);

//Echo to client
ret = sendto(server , buff , strlen(buff) , 0,(SOCKADDR *)
&clientAddr,sizeof(clientAddr)); if(ret == SOCKET_ERROR )
printf("Error: %", WSAGetLastError());

//end while

//Step 5: Close socket


closesocket(server);

//Step 6: Terminate Winsock


WSACleanup();
return 0;

You might also like