Pengiriman dan penerimaan data via TCP Socket pada C++

Posted: January 13, 2013 by Iman Rahmansyah Tayibnapis in C++, socket programming
Tags:

Pengiriman dan pengiriman  data merupakan bagian terpenting untuk berkomunikasi antara client dengan server.  Dalam proyek ini, client akan mengirimkan data-data hasil pengukuran kesehatan melalui suatu perangkat yang kami buat.  Salah satu pernakngkat yang dibuat menggunakan bahasa pemrograman C++, maka dari itu dibuatlah pemrograman soket TCP pada C++.
Hal yang penting dilakukan adalah memasukan library ws2_32.lib dan WinSock2.h untuk komuniskasi soket TCP.
Berikut kode program sederhananya:

#include
#include
#include
#include
#include
#pragma comment(lib, "ws2_32.lib")

using namespace std;
int main(int argc , char *argv[])
{
    WSADATA wsa;
	SOCKET s;
	struct sockaddr_in server;
	char* message, server_reply[2000];
    int recv_size;

	char *hostname = "localhost";//"course.lskk.ee.itb.ac.id";
    char ip[100];
    struct hostent *he;
    struct in_addr **addr_list;
    int i;

	/* Initialising / Loading Winsock library */
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
    printf("Initialised.");

	/*read ip host name*/
	if ( (he = gethostbyname( hostname ) ) == NULL)
    {
        //gethostbyname failed
        printf("gethostbyname failed : %d" , WSAGetLastError());
        return 1;
    }

    //Cast the h_addr_list to in_addr , since h_addr_list also has the ip address in long format only
    addr_list = (struct in_addr **) he->h_addr_list;
    for(i = 0; addr_list[i] != NULL; i++)
    {
        //Return the first one;
        strcpy(ip , inet_ntoa(*addr_list[i]) );
    }
    printf("%s resolved to : %s\n" , hostname , ip);

	/* Create socket */
	if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }
    printf("Socket created.\n");

	server.sin_addr.s_addr = inet_addr("192.168.109.1");//ip);//"167.205.56.188");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8080 );

	////Bind
 //   if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
 //   {
 //       printf("Bind failed with error code : %d" , WSAGetLastError());
 //   }
 //   puts("Bind done");

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
		getchar();
        return 1;
    }
    puts("Connected");

	//Send some data
    //message = "test je";//"GET / HTTP/1.1\r\n\r\n";
    //if( send(s , message , strlen(message) , 0) < 0)
    //{
    //    puts("Send failed");
    //    return 1;
    //}
    //puts("Data Send\n");
	int a=12;
	char x[33];

	itoa(a,x,10);
	char y[100]="*0001*CAM*H";
	strcat(y,x);
	strcat(y,"#");
	cout<<"msg: "<<message<<endl;
	send(s , y , strlen(y) , 0);

	//Receive a reply from the server
	if((recv_size = recv(s,server_reply,2000,0)) == SOCKET_ERROR)
    {
        puts("recv failed");
    }
	else
		puts("Reply received\n");

    //recv_size = recv(s,server_reply,2000,0);

	//Add a NULL terminating character to make it a proper string before printing
    server_reply[recv_size] = '\0';
    puts(server_reply);

	getchar();
    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s