Pages

Subscribe:

Ads 468x60px

Sunday, December 1, 2013

How to Build WSO2 Carbon 4.2.0 Tutorial for a newbie

Hi all, in this post I am going to describe how to build WSO2 carbon from source. Now carbon is moving towards it's newest version carbon 5 (C5). 

prerequisites

1. Linux (Mine is Ubuntu 13.04)
2. Oracle Java 1.6 (1.7 is still not supported)
3. Apache Maven 3.0.5 (see post for installing apache maven)
4. Subversion

I assume that you have installed and configured all of the above and ready to build carbon. :)

Create a directory called carbon. Inside that folder create three directories called orbit, kernel and platform. If you need to build WSO2 carbon you first need to build orbit, kernel and the platform. You may be confused and wondering what is this orbit, kernel and platform is. Orbit is the third party external dependencies which needed for carbon. Kernel is the core part of the carbon which other wso2 products are run upon. Platform contains the other products of wso2.

carbon --- > orbit
            --- > kernel
            --- > platform

Ok first move into the orbit folder and checkout the source. To do so execute the following command.

svn checkout http://svn.wso2.org/repos/wso2/carbon/orbit/branches/4.2.0/

Then move into the kernel folder and checkout the source. To do so execute the following command.

svn checkout http://svn.wso2.org/repos/wso2/carbon/kernel/branches/4.2.0/

And finally move into the platform folder and checkout the platform source.

svn checkout http://svn.wso2.org/repos/wso2/carbon/platform/branches/turing/

Before building from source there are few settings to be applied. open the .bashrc file using the following command.

gedit /home/HOME_DIR/.bashrc

and append  the following lines.

MAVEN_OPTS=" -Xms512m -Xmx1024m -XX:MaxPermSize=1024m"
export MAVEN_OPTS

now build the following order; orbit -->  kernel and finally platform.
Move into the orbit folder and you'll see pom.xml file. Enter the following command to build the orbit.

mvn clean install -Dmaven.test.skip=true

Note that to complete the build you need to connect to the internet. Now that's it. There may be some build failures. Try updating the subversion and rebiuld.

In the next tutorial I'm going to explain how to debug the carbon kernel. See you until then.

Friday, November 29, 2013

How to install Apache Maven in Ubuntu 13.04

In this post I'm going to explain n how to setup apache maven on your ubuntu box. 

First Download the apache-maven-3.0.5-bin.tar.gz from the http://maven.apache.org/download.cgi website.

Then go to the downloaded folder and extract the downloaded bundle using the following command.

tar -zxf apache-maven-3.0.5-bin.tar.gz

Then copy that folder where ever you want in your computer. In this example I copy it to my home folder.

sudo cp -R apache-maven-3.0.5 /home/aruna

Then the final step is to create the symbolic link to the maven executable. Use the following command to create the symbolic link.
sudo ln -s /home/aruna//apache-maven-3.0.5/bin/mvn /usr/bin/mvn  

Now you are almost done and execute the following command to check the installation.
mvn –version  

Then you'll probably get the following output.

Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 19:21:28+0530)
Maven home: /home/aruna/apache-maven-3.0.5
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-12-generic", arch: "amd64", family: "unix"
That's it and see you in another post.  :)

Monday, May 6, 2013

Enable Offline data in Google Maps for Android

Hi guys it's been long time since I wrote a blog post. So now in this post I'm going to explain how to use google maps data for offline usage. This will be very useful for people who is limit with their mobile data budgets.

So first we need ti install the application from the Google Play https://play.google.com/store/apps/details?id=com.google.android.apps.maps&hl=en

Then enable your wifi connection. This will take around 80 MB maximum for the selected map area.

Left Click --> Select Offline Maps. Select New Offline Map and Select the area you want offline data. The size will be vary according to your selection of area.

Now enjoy Google offline maps in your android device..


Saturday, February 9, 2013

Non Blocking I/O using SELECT System Call in Linux

Today we are going to explore a very important system call in Linux, The Select system call. Select System call is use  when we need non blocking executions. The use of the select can be explained via an example code. Lets assume that we use a socket program with a server and few client programs. To accept and read from clients what will be your normal approach?.. Probably you are checking for new clients and the data from the existing clients and probably use a read system call. Here  the approach is blocking, the program is blocked for events. But using the select system call, it can be achieved in a non- blocking manner. Here we put the connected clients along with the server into a fd_set type variable. Then the select system  call is looking for any read events for each clients and read the data non bloc-kingly.

Lets see an example code for this scenario.

In this example there is a server which is binded to port 9999. and clients can be connected to that port. Then the server identify whether a new client or data from an existing  client. Select call will monitor the read_fds and act immediately for a change of a read file descriptor. There is a time out value for select system call.

In this example many clients are connected to the server and heart beats are send for the clients for every 3 seconds. And from the server we can send work(In this scenario messages) for clients. Then the clients work on those messages and send result to the server. We can non- bloc-kingly send data to clients and read data from clients using the select system call. :) 

Server.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <time.h> 
#include "Data.h"

#define PORT "9999"   // port we're listening on
#define MAX_MESSAGE_SIZE 50 // Maximum message size

pthread_mutex_t mutexSync = PTHREAD_MUTEX_INITIALIZER;

int signal =0;
char Buffer[1024];

int Initialize(void);
void *Communicate(void *);
void *get_in_addr(struct sockaddr *sa);
void AcceptNewClients(fd_set &master , int &fdmax ,int listener);
void AcceptDataFromClients(int iClient , fd_set & master);
void SendHeartBeats(int iListener , int iClient);
void SendWorkForClients( int fdmax , int iListener , int iClient , int &signal);



//****************************************************************************************************
int main(void)
{
	pthread_t t;
	// char Buffer[256];
	int iListenSockId = Initialize();
	//Communicate(&iListenSockId);
	pthread_create(&t , NULL, Communicate , (void *)&iListenSockId); 
	while(1)
	{
		printf("Enter a message to Send :: ");
		fgets(Buffer , 255 , stdin);
		pthread_mutex_lock(&mutexSync);
		signal++;
		//printf("Message is %s %d \n", Buffer , signal);
		pthread_mutex_unlock(&mutexSync);
		sleep(1);
		//memset(&Buffer[0], 0, sizeof(Buffer));
			
	}
	
    return 0;
}

//****************************************************************************************************
int Initialize()
{
	int listener ;     // listening socket descriptor
	int rv ;
	struct addrinfo hints, *ai, *p;
	int yes=1;        // for setsockopt() 
	 // get us a socket and bind it
    
	memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) 
	{
        fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
        exit(1);
    }
    
    for(p = ai; p != NULL; p = p->ai_next) 
	{
        listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
        if (listener < 0) 
		{ 
            continue;
        }       
        // lose the pesky "address already in use" error message
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
        if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) 
		{
            close(listener);
            continue;
        }
        break;
    }
    // if we got here, it means we didn't get bound
    if (p == NULL) 
	{
        fprintf(stderr, "selectserver: failed to bind\n");
        exit(2);
    }
    freeaddrinfo(ai); // all done with this
    // listen
    if (listen(listener, 10) == -1) {
        perror("listen");
        exit(3);
    }
	
	return listener;

}

//****************************************************************************************************
void * Communicate(void * id)
{
	int *iSockID = (int *) id;
	int listener =  *iSockID;

    fd_set master;    // master file descriptor list
    fd_set read_fds;  // temp file descriptor list for select() read
    int fdmax;        // maximum file descriptor number

    int i, j, rv;

    FD_ZERO(&master);    // clear the master and temp sets
    FD_ZERO(&read_fds);
    // add the listener to the master set
    FD_SET(listener, &master);
	printf("Listener is %d \n" , listener);

    // keep track of the biggest file descriptor
    fdmax = listener; // so far, it's this one
	//accept 3 clients

	
    // main loop
    for(;;) {
		memcpy(&read_fds , &master , sizeof(master));
		struct timeval tv;
		tv.tv_sec = 3;
		tv.tv_usec = 0;
		int iResult = select(fdmax+1, &read_fds, NULL, NULL, &tv) ;
        if (iResult == -1) 
		{
            perror("select");
            exit(4);
        }
	
		// ADD NEW CONNECTIONS READ FROM CONNECTIONS	
		for(i = 0; i <= fdmax; i++)
		{			   
            if (FD_ISSET(i, &read_fds)) 
			{			
				if (i == listener) 
				{                                    
					AcceptNewClients(master , fdmax , listener );	
                } else 
				{
					AcceptDataFromClients(i , master);
                } 
            } 

			
        } 
        for(i = 0; i <= fdmax; i++) 
		{			
			//send work for clients
			SendWorkForClients(fdmax , listener , i , signal);
			//sending heart beats
		   	SendHeartBeats(listener , i );
		}

    } 
	return 0;
}

//****************************************************************************************************
void SendWorkForClients(int fdmax , int iListener , int iClient , int &signal)
{

		if(signal == 1 && iClient != iListener && iClient > iListener)
		{
			//send data to some client;
			//printf("data send to CLIENT DUMMY \n");
			//signal--;
			ServerData * data = new ServerData();
			data->iType = 2;
			strcpy(data->cMessage , Buffer );
			int numbytes  = write( iClient , data ,sizeof(*data));
			printf("Number of bytes written :: %d ID :: %d  \n" , numbytes , iClient);
		
			delete data;
		}
		if(signal == 1 && iClient != iListener && iClient > iListener && iClient == fdmax)
		{
			pthread_mutex_lock(&mutexSync);
			signal--;
			pthread_mutex_unlock(&mutexSync);
		}
	
}

//****************************************************************************************************
void SendHeartBeats(int iListener , int iClient)
{
	ServerData * data = new ServerData();
	time_t rawtime;
	time ( &rawtime );

	data->iType = 1;
	strcpy(data->cMessage , ctime (&rawtime)  );
	//char cHeartBeat []  = "HEARTBEAT";


		if(iClient!= iListener && iClient > iListener){
			int numbytes  = write( iClient , data , sizeof(*data)); 
			//printf("Number of bytes written :: %d ID :: %d  \n" , numbytes , i);
		}					
	
	delete data;
}
//****************************************************************************************************
void AcceptNewClients(fd_set&  master , int& fdmax ,int listener)
{
		socklen_t addrlen;
		struct sockaddr_storage remoteaddr; // client address
		char remoteIP[INET6_ADDRSTRLEN];		
		int newfd;        // newly accept()ed socket descriptor
		addrlen = sizeof(remoteaddr);
		ServerData * data = new ServerData();
		data->iType = 2;
		strcpy(data->cMessage , "SERVER MESSAGE :: ACCEPTED" );
        newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
		write(newfd , data , sizeof(*data));
		if (newfd == -1) 
		{
            perror("accept");
        }else 
		{
            FD_SET(newfd, &master); // add to master set
            if (newfd > fdmax) 
			{    // keep track of the max
				fdmax = newfd;
            }
            printf("selectserver: new connection from %s on socket %d\n", inet_ntop(remoteaddr.ss_family,
                                get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd);
        } 
		
		delete data;
}

//****************************************************************************************************
void AcceptDataFromClients(int iClient , fd_set& master)
{
	char buf[1024];    // buffer for client data
	int nbytes;
	
	nbytes = recv(iClient, buf, sizeof buf, 0);
	//printf("DISCONNECTING  %d \n", nbytes);
    if (nbytes <= 0) 
	{
    // got error or connection closed by client
		if (nbytes == 0) 
		{
			// connection closed
			printf("selectserver: socket %d hung up\n", iClient);
		} else 
		{
			perror("recv");
		}
        close(iClient); // bye!
        FD_CLR(iClient, &master); // remove from master set
    } else 
	{
    // we got some data from a client
		printf("CLIENT DATA :: %s",buf);
                       
    }

}

//****************************************************************************************************
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) 
	{
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

Client.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include "Data.h"


int main(int argc , char * argv [])
{
	int SockFD , PortNo , n , fdmax ,nbytes;
	struct sockaddr_in Server_Address;
	struct hostent *Server;
	fd_set master;    // master file descriptor list
    fd_set read_fds;  // temp file descriptor list for select()
	FD_ZERO(&master);    // clear the master and temp sets
    FD_ZERO(&read_fds);

	char Buffer[1024];
	memset(Buffer, 0, sizeof(Buffer));

	if(argc < 3)
	{
		printf("Error hostname port required \n");
		exit(0);
	}

	PortNo = atoi(argv[2]);

	// create a socket	
	SockFD =  socket(AF_INET , SOCK_STREAM , 0);

	if(SockFD < 0 )
	{
		perror("Error Creating Socket");
		exit(1);
	}
	
	Server = (struct hostent *) gethostbyname(argv[1]);
	if(Server == NULL)
	{
		printf("Error :: No Such Host \n");
		exit(0);
	}

	bzero((char *) &Server_Address , sizeof(Server_Address));
	Server_Address.sin_family =  AF_INET;
	bcopy((char *) Server->h_addr , (char *) &Server_Address.sin_addr.s_addr , Server->h_length);
	
	Server_Address.sin_port = htons(PortNo);
	
	//connect to the server
	int listener = connect(SockFD , (struct sockaddr *) &Server_Address , sizeof(Server_Address));
	if( listener < 0)
	{
		perror("Error Connecting \n");
		exit(1);
	}
	

	while(1)
	{	
		n = read(SockFD , Buffer , sizeof(Buffer));
		ServerData * data = new ServerData();
		data = (ServerData *) Buffer;
		
		if(n <= 0)
		{
			perror("Error receiving data \n");
			exit(1);
		}
		//int result = strcmp( Buffer, "HEARTBEAT" );
		if(data->iType == 2)
		{
			//sleep(1);	
			printf("Received :: %s", data->cMessage);
			write(SockFD , "CLIENT MESSAGE-->ACCEPTED" ,26);
		}	
		else if(data->iType == 1 )
		{
			//sleep(2);	
			printf("Heart Beating Server::  %s", data->cMessage);
		}
		memset(Buffer, 0, sizeof(Buffer));
		//
		
	}

	return 0;

}

Data.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Header file for data
// type 1 -> Heart Beat Message
// type 2 -> Server Task Message

class ServerData
{
	public:
	int iType;
	char cMessage [50];
};

To compile and run the server...

1
2
3
g++ Select.cpp -lpthread

./a.out

To compile and run the client...

1
2
3
g++ -o client client.cpp

./client localhost 9999


That's it folks. If you have anything to clarify just put a comment. I'll answer for the best of my knowledge.

Here is a nice tutorial for Linux system calls. I recommend you all to read it.
http://www.beej.us/guide/bgnet/output/html/multipage/index.html


Wednesday, January 23, 2013

Producer Consumer Problem in C++

Today I'm going to solve a most common problem which is known as the consumer producer problem. In this context we have a shared buffer which the producers produce and the consumers consume. The consumers and producers are threads which will simultaneously produce and consume. There are some conditions to be met where consumers have to wait until producers produce, and another thing is that when the buffer is full producers must halt until the consumers consume.

In the following example was implemented using pthreads. The producer produce a random number to the buffer and consumers consume that number. Mutex locks are used to protect the shared buffer. 
ProducerConsumer.cpp     DOWNLOAD SOURCE

#include <iostream>
#include <pthread.h>
#include <vector>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>



#define BUFFER_SIZE 10

void InitializeData();
void *Produce(void *);
void *Consume(void *);
int InsertItem(int);
int RemoveItem(int *);

int iCounter;
pthread_mutex_t mutex;
sem_t full, empty;

int buffer[BUFFER_SIZE];



int main(int argc , char * argv[])
{
 InitializeData(); 
 pthread_t ProducerThread , ConsumerThread;
 int *aa = new int [10];
 for(int i = 0 ; i < 10 ; i++)
 {
  aa[i] = i;
  pthread_t t;
  pthread_create(&t , NULL, Produce , &aa[i]);
  printf("Creating Producer %d \n", i);
 }
 int *bb = new int[10];
 for(int i = 0 ; i < 10 ; i++)
 {
  bb[i] = i;
  pthread_t t;
  pthread_create(&t , NULL, Consume , &bb[i]);
  printf("Creating Consumer %d \n", i);
 }
 
 sleep(5);
 delete [] aa;
 delete [] bb;
 

 return 0;
}

//****************************************************************************************************
void InitializeData()
{
 pthread_mutex_init(&mutex , NULL);
 sem_init(&full , 0 ,0);
 sem_init(&empty , 0 , BUFFER_SIZE);
 
 iCounter = 0;
 

}

//****************************************************************************************************
void * Produce(void * Param)
{
 int item;
 
 while(1)
 {
  //sleep(1);
  item = rand() % 100;
  sem_wait(&empty);
  pthread_mutex_lock(&mutex);
  int iMsg = InsertItem(item);
  
  if(iMsg == -1){
   printf("Error Inserting Item \n");
  }else
  {
   printf("Produced Item :: %d  Thread No :: %d\n", item , *((int *)Param));
  }
  pthread_mutex_unlock(&mutex);
  sem_post(&full);
  
 }
}

//****************************************************************************************************
void * Consume(void * Param)
{
 int item;
 
 while(1)
 {
  //sleep(1);
  sem_wait(&full);
  pthread_mutex_lock(&mutex);
  int iMsg = RemoveItem(&item);
  
  if(iMsg == -1){
   printf("Error Removing Item \n");
  }else
  {
   printf("Consumed Item :: %d  Thread No :: %d \n", item ,*((int *)Param));
  }
  pthread_mutex_unlock(&mutex);
  sem_post(&empty);
  
 }
}

//****************************************************************************************************
int InsertItem(int item)
{
 if(iCounter < BUFFER_SIZE)
 {
  buffer[iCounter] =  item;
  iCounter++;
  return 1;
 }
 else{
  return -1;
 }

}

//****************************************************************************************************
int RemoveItem(int *item)
{
 if(iCounter > 0)
 {
  *item = buffer[iCounter - 1];
  iCounter--;
  return 1;
 }
 else{
  return -1;
 }
}

That's it folks. Hope to see you soon in another exciting tutorial.

Friday, January 18, 2013

How to send Complex Data Structures in a Socket Program

Hi all, Hope you guys are doing well. I was busy with my work last few days. Today I'm going to write a post about sending Complex data structures using a socket program. In this post I assume that you are somewhat familiar with the socket programming basics.

In the following example we are creating a Object type in the server program and send that to the client program. This is a simple serialize and deserialize mechanism to send complex data structures through a socket program using only the STL library. If you want more complex and complete functionality you might try the boost library. 

Here we are going to send the following data structure.
 Data.h File
class InnerData{
    public:
    int c;
};

class Data{
    public:
    int a;
    char b[20];
    InnerData id;
  
};

Now we write the Server program which will create a socket, bind that socket to a port and listen for incoming connections. If there is a incoming connection accept the connection, serialize the data send to the connected client.

Server.cpp File
#include <stdio.h>
#include <cstdlib> 
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <arpa/inet.h> 
#include "Data.h"

int main(int argc , char * argv [])
{
    int SockFD , NewSockFD , PortNo, Clilen;
    char Buffer[1024];
 struct sockaddr_in Server_Addr , Cli_Addr;
 int n , pID;

 //create a socket
 SockFD =  socket(AF_INET , SOCK_STREAM , 0);
 if(SockFD < 0)
 {
  perror("Error Creating Socket \n");
  exit(1);
 }
 std::cout << "Socket Created..." << std::endl;

 bzero((char *) &Server_Addr , sizeof(Server_Addr));
 PortNo = atoi(argv[1]);
 Server_Addr.sin_family = AF_INET;
 Server_Addr.sin_addr.s_addr = INADDR_ANY;
 Server_Addr.sin_port = htons(PortNo);

 
 //bind the socket
 if(bind(SockFD , (struct sockaddr *) &Server_Addr , sizeof(Server_Addr)) < 0)
 {
  perror("Error Binding \n");
  exit(1);
 }
 std::cout << "Socket Binded..." << std::endl;
 //listen for incoming clients
 listen(SockFD , 5);
 Clilen =  sizeof(Cli_Addr);
 std::cout << "Waiting For Connections..." << std::endl;
 
 while(1)
 {
  //accept a client 
  NewSockFD = accept(SockFD , (struct sockaddr *) &Cli_Addr ,(socklen_t *) &Clilen);
  printf("New Client %d \n", NewSockFD);
  if(NewSockFD < 0)
  {
   perror("Error accepting \n");
   exit(1);
  }  
   bzero(Buffer , 1024);
   printf("New Client Connected, Process ID :: %d \n",pID);
   //create the Data
   Data *data = new Data();
   data->a = 23232;
   strcpy(data->b , "TEST MESSAGE" );
   data->id.c =1000;
  
   n =  write(NewSockFD , data ,sizeof(*data) );
   if(n < 0)
   {
    perror(" Error Sending Message \n");
    exit(1);
   }
  
 }
 return 0;
}

Now the client accept the data send by the client and deserialize it.

Client.cpp
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h> 
#include "Data.h"


int main(int argc , char * argv [])
{
 int SockFD , PortNo , n;
 struct sockaddr_in Server_Address;
 struct hostent *Server;
 
 char Buffer[1024]; // Buffer to read data

 if(argc < 3)
 {
  printf("Error hostname port required \n");
  exit(0);
 }
 PortNo = atoi(argv[2]);
 // create a socket 
 SockFD =  socket(AF_INET , SOCK_STREAM , 0);

 if(SockFD < 0 )
 {
  perror("Error Creating Socket");
  exit(1);
 }
 
 Server = (struct hostent *) gethostbyname(argv[1]);
 if(Server == NULL)
 {
  printf("Error :: No Such Host \n");
  exit(0);
 }

 bzero((char *) &Server_Address , sizeof(Server_Address));
 Server_Address.sin_family =  AF_INET;
 bcopy((char *) Server->h_addr , (char *) &Server_Address.sin_addr.s_addr , Server->h_length);
 
 Server_Address.sin_port = htons(PortNo);
 
 //connect to the server
 if(connect(SockFD , (struct sockaddr *) &Server_Address , sizeof(Server_Address)) < 0)
 {
  perror("Error Connecting \n");
  exit(1);
 }
 Data * data = new Data();
 n = read(SockFD , Buffer , sizeof(Data));
 data = (Data *) Buffer;
 printf("Read Size %d \n" , n);
 if(n < 0)
 {
  perror("Error receiving data \n");
  exit(1);
 }
 printf("Received :: ID :: %d  MESSAGE :: %s Complex ID:: %d \n", data->a , data->b ,data->id.c );
 bzero(Buffer , 1024);

 return 0;

}

First we compile the Server.cpp file
g++ -o Server Server.cpp

Then we run the server giving the port as an argument.
./Server  7788

Then compile and run the client program using a new terminal.
g++ -o Client Client.cpp

./Client localhost 7788

That's it folks. It's a simple approach. But solve the problem. Remember that this only works since we are using the same computer architecture. If you are using different architectures you have to consider about the endian problem also.. :)