You aren't log on!| [ Register ]
Google

Simple TCP/IP Client using winsock library

This example of TCP/IP client I did like small project to my subject which is called Instruments of industrial automation.

Category: Project | 2008-10-04 22:21:23 | Read: 1876x | Rating: 3.6 of 5.0

The my TCP/IP client will connect to World Time Server("64.236.96.53") and will recieve 4 bytes of time data.Then data are transformed to HH:MM:SS and printed to console.

Source Code

#include "winsock2.h"
#include "stdio.h"
#include "conio.h"
#include "windows.h"

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


int main()
{
typedef union {
unsigned int time;
unsigned char time_buf[4];
} mytime;

WORD wVersionRequested;
WSADATA wsaData;
int err;
SOCKADDR_IN target;
SOCKET sock;
char buffer[30];
int result;
mytime clock;

/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);

err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %dn", err);
getchar();
return -1;
}

//create socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

//if creation of socket failed
if(sock == INVALID_SOCKET)
{
printf("Error at socket : %ld n",WSAGetLastError());
WSACleanup();
getchar();
return -1;
}

target.sin_family = AF_INET; //define type of communication protocol
target.sin_addr.S_un.S_addr = inet_addr("64.236.96.53"); //set IP adress of Server
target.sin_port = htons(37); //set communication port

//head of client
printf("BPPA_TCP_CLIENT_v1.0n");
printf("-----------------------n");

//connect to server with IP 64.236.96.53 - World Time Server

if(connect(sock,(SOCKADDR*)&target, sizeof(target)) == SOCKET_ERROR)
{
//if connection to server failed
printf("Connection lost.n");
closesocket(sock);
WSACleanup();
return -1;
}
else
{
printf(" Connected.r");
Sleep(2000);
}

//recieving time data from server
do{
result = recv(sock, buffer, sizeof(buffer), 0);
if ( result > 0 )
{
//simple loader - there have any
printf(" Recieving data.n");
Sleep(1000);
printf(" = 20%c r",37);
Sleep(1000);
printf(" == 40%c r",37);
Sleep(1000);
printf(" === 60%c r",37);
Sleep(1000);
printf(" ==== 80%c r",37);
Sleep(1000);
printf(" ===== 100%cr",37);
Sleep(1000);
printf(" Bytes received: %dn", result);

//transform recieved data from big endian to little endian
clock.time_buf[0] = buffer[3];
clock.time_buf[1] = buffer[2];
clock.time_buf[2] = buffer[1];
clock.time_buf[3] = buffer[0];
unsigned int buf = clock.time;

//transform recieved data to hours:minutes:seconds
int SS = buf % 60; buf = buf / 60;
int MM = buf % 60; buf = buf / 60;
int HH = buf % 60; buf = buf / 60;
printf("-----------------------n");
printf(" %0.2d : %0.2d : %0.2d GMT n",HH+2,MM,SS);
printf("-----------------------n");
}
else if ( result == 0 )
printf(" Connection closed.n");
else
printf("recv() failed: %dn", WSAGetLastError());
}while(result > 0);

/* then call WSACleanup when down using the Winsock dll */
WSACleanup();

getchar();
return 0;
}




TCP/IP client console

More information about winsock functions you can find there:
Winsock help


   tcp_client source file

      < Previous | Back To Index | Next >

Add Comment:

User:

Comment:

:-)
:-D
:-(
|-/
:-[]
;-)
8-|
8-o
B | U | i  | code  | odkaz

login:
password:
TOPlist

Valid CSS!

Valid HTML 4.01 Transitional