Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
client program:
C
#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;                           //create the object for the SOCKET 
	WORD Data = MAKEWORD(2,2);
	WSADATA dataword;
	long inputservernamelong;
	struct hostent *remotehost;
	int integerportnumber;
	char servername[1000],*temporary ;             //Declare the required variables
	struct in_addr addr;
	WSAStartup(Data,&dataword);			            //initiates the Winsock DLL by a process.
	clientsocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); //create the scoket for udp communication
	if(INVALID_SOCKET == clientsocket)	            //checking the socket
		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);           //resolve the hostname
		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); //send the data to the server
	printf("\nThe server respons\n");
	recvfrom(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,&clientsize); //receive the data from the server
	printf("\nThe current time is %s\n\n",buffer);
	closesocket(clientsocket);                      //close the socket
	WSACleanup();							        //clean the winsockDll
	return 0;
}


server program:

C
// udps.cpp : Defines the entry point for the console application.
//

#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;			//create the object for the SOCKET structure
	WSADATA wdata;
	time_t timevariable;
	int sendersize;	
	char *buffer=new char[100],servername[1024];;				//Declare the required variables
	int portnumber;
	time(&timevariable);
	//define the time function
	WORD startup=MAKEWORD(2,2);		
	WSAStartup(	startup,&wdata);	//Intialise the winsock dll
	serversocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);//create the socket for the udp server
	if(serversocket == INVALID_SOCKET)
		printf("Error on socket Creation\n");
	else
		printf("Socket Sucessfully Created\n");
	sockaddr_in serverconfiguration;
	serverconfiguration.sin_family = AF_INET;
	//gethostname(buffer,sizeof(buffer));
	//strcpy(servername,buffer);
	node* ipaddress;		                //create the pointer for the dll that get the ipaddress of the system
	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);//initialize the serverconfigurations
	bind(serversocket,(SOCKADDR*)&serverconfiguration,1000);		//Bind the server socket
	sendersize = sizeof(serverconfiguration);
	while(1)
	{
		printf("The server is waiting...\n");
		ZeroMemory(buffer,sizeof(buffer));	//make the buffer memory as zero
		recvfrom(serversocket,buffer,sizeof(buffer),0,(sockaddr*)&serverconfiguration,&sendersize);//receives the request from the client
		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);
		//char *currenttime;
	//	currenttime=ctime(&timevariable);  //Get the current time form the system
		//sendto(serversocket,currenttime,strlen(currenttime),0,(sockaddr*)&serverconfiguration,sendersize); //responce to the client
		//printf("\nThe current time sent to the client\n");
		printf("\n The service over\n\n\n");
	}
	closesocket(serversocket);
	WSACleanup();               //close the socket and clean the winsock dll
	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..
Posted
Updated 13-Jul-11 3:13am
v2

aimdharma wrote:
sendto(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,clientsize); //send the data to the server

There's a mistake in the above line, you should use sizeof(cli) (if you want to send just 1 item) instead of sizeof(buffer).

Please note: apparently you are not very familiar with dynamic memory handling, the following lines show it:
aimdharma wrote:
cli *o;
o=new cli[100];
o->data=10;
o->data1=11;
 
Share this answer
 
Comments
@BangIndia 13-Jul-11 9:27am    
may i use like that sir..

cli *o=new cli[10];
Richard MacCutchan 13-Jul-11 11:52am    
Why do you want 10 of them when you only use one?
CPallini 13-Jul-11 12:09pm    
You may do better, for instance:
cli o;
o.data = 10;
o.data1 = 11;
//...
sendto(clientsocket, (char *)&o, sizeof(o), 0, (sockaddr*)&client,&clientsize);

BTW: what esoteric 'name convention' are you following?
@BangIndia 13-Jul-11 9:42am    
thanks for the help..
sendto(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,clientsize); //send the data to the server

buffer is defined as a char* and therfore its size is the size of a char* which is 4 bytes (assuming 32 bit architecture). But you are trying to send a copy of the data in a struct cli which is somewhat larger. Make sure you are specifying the size of the correct object in your function calls.

As an observation the pointer buffer is redundant as it is merely used to hold the address of your structure o which already exists. It would make more sense (and readability) to use a cast in your function call. Also when you create your cli object pointer you create an array of 100 items, but you only require a single one for this code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900