client program:
#include "stdafx.h"
#include <winsock2.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
struct cli
{
int data;
int data1;
};
int main(int argc, char* argv[])
{
cli *o;
o=new cli[100];
o->data=10;
o->data1=11;
SOCKET clientsocket; WORD Data = MAKEWORD(2,2);
WSADATA dataword;
long inputservernamelong;
struct hostent *remotehost;
int integerportnumber;
char servername[1000],*temporary ; struct in_addr addr;
WSAStartup(Data,&dataword); clientsocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(INVALID_SOCKET == clientsocket) printf("The socket creation is invalid\n");
else
printf("Created sucessfully\n");
printf("\nEnter the server name \n");
scanf("%s",&servername);
printf("\nEnter the port number to be communicate to the client\n");
scanf("%d",&integerportnumber);
inputservernamelong=inet_addr(servername);
if(inputservernamelong==-1)
{
printf("Resolving the Hostname\n");
remotehost = gethostbyname(servername); addr.s_addr = *(u_long *) remotehost->h_addr_list[0];
temporary=inet_ntoa(addr);
}
else
temporary=servername;
sockaddr_in client;
client.sin_family = AF_INET;
client.sin_port = htons(integerportnumber);
printf("The server address is%s\n\n",temporary);
client.sin_addr.s_addr = inet_addr(temporary);
char *buffer;
int clientsize = sizeof(client);
buffer=(char*)o;
sendto(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,clientsize); printf("\nThe server respons\n");
recvfrom(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,&clientsize); printf("\nThe current time is %s\n\n",buffer);
closesocket(clientsocket); WSACleanup(); return 0;
}
server program:
#include "stdafx.h"
#include <winsock2.h>
#include <string.h>
#include <time.h>
#include "newdynamciip.h"
struct cli
{
int data;
int data1;
};
int main(int argc, char* argv[])
{
cli *o1;
SOCKET serversocket; WSADATA wdata;
time_t timevariable;
int sendersize;
char *buffer=new char[100],servername[1024];; int portnumber;
time(&timevariable);
WORD startup=MAKEWORD(2,2);
WSAStartup( startup,&wdata); serversocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(serversocket == INVALID_SOCKET)
printf("Error on socket Creation\n");
else
printf("Socket Sucessfully Created\n");
sockaddr_in serverconfiguration;
serverconfiguration.sin_family = AF_INET;
node* ipaddress; ipaddress = myfun();
strcpy(servername,ipaddress->ip);
printf("\nThe server name is %s\n",servername);
printf("Enter the port number\n");
scanf("%d",&portnumber);
serverconfiguration.sin_port = htons(portnumber);
serverconfiguration.sin_addr.s_addr = inet_addr(servername); bind(serversocket,(SOCKADDR*)&serverconfiguration,1000); sendersize = sizeof(serverconfiguration);
while(1)
{
printf("The server is waiting...\n");
ZeroMemory(buffer,sizeof(buffer)); recvfrom(serversocket,buffer,sizeof(buffer),0,(sockaddr*)&serverconfiguration,&sendersize); o1=(struct cli*)buffer;
int aa=o1->data;
int aa1=o1->data1;
printf("the value is \n\n%d\n",aa1);
printf("the data part is :: %d\nThe name part is %d",aa,o1->data1);
printf("\n The service over\n\n\n");
}
closesocket(serversocket);
WSACleanup(); return 0;
}
i declare one structure in the client side and i initialize some data to that structure.
i send it to the server...
while i try to print in the server only the data variable's value only coming..
the data1 value not coming..
please what i want to do now..