帮助我了解UDP服务器和客户端的概念 [英] help me about the concept of UDP server and client
问题描述
客户端程序:
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; //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:
// 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;
}
我在客户端声明了一个结构,然后将一些数据初始化为该结构.
我将其发送到服务器...
而我尝试在服务器中仅打印数据变量的值时才开始打印..
data1值不来.
请我现在要做什么..
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..
推荐答案
sendto (clientsocket,buffer,sizeof(buffer),0,(sockaddr *)& client,clientsize); //将数据发送到服务器
sendto(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,clientsize); //send the data to the server
上一行有一个错误,您应该使用sizeof(cli)
(如果您只想发送1个项目)而不是sizeof(buffer).
请注意:很显然,您对动态内存处理不是很熟悉,以下几行显示了它:
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:
cli * o;
o = new cli [100];
o-> data = 10;
o-> data1 = 11;
cli *o;
o=new cli[100];
o->data=10;
o->data1=11;
sendto(clientsocket,buffer,sizeof(buffer),0,(sockaddr*)&client,clientsize); //send the data to the server
buffer
被定义为char*
,因此其大小是char*
的大小,该大小为4个字节(假定为32位体系结构).但是,您试图在struct cli
中发送数据的副本,该副本有些大.确保在函数调用中指定了正确对象的大小.
观察到,指针buffer
是多余的,因为它仅用于保存已经存在的结构o
的地址.在函数调用中使用强制转换会更有意义(也更易于理解).同样,在创建cli
对象指针时,还会创建一个包含100个项目的数组,但是此代码只需要一个即可.
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.
这篇关于帮助我了解UDP服务器和客户端的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!