2012-03-12 68 views
0

我还有一个问题:)我在C读取一些字节,一个简单的UDP服务器应用一些解码这些字节,当它具有形式为### @ ##一STRING ## @ ### @ ###他通过UDP将它发送到C中的另一个服务器。以下是我的C服务器的代码,称为preprocesamiento.c Im发布整个事情cos是easyer,但也许这没有任何关系与我的问题。UDP连接

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <math.h> 
#include <stdint.h> 
#include <string.h> 
#include <openssl/sha.h> 
#include <openssl/hmac.h> 
#include <openssl/evp.h> 
#include <openssl/bio.h> 
#include <openssl/buffer.h> 
#define MAXBUF 512 
#define SENDING 0x52 
#define RESIVING 0xB4 
#define TYPE 0xA3F0 

int createSocket(); 
char *unbase64(unsigned char *input, int length); 
/* This function recives bytes(ASCII numbers) and returns the char */ 
void cambiarAChars(char* bytes, char* result) 

    { 

unsigned int ch; 

    char a[4]; 
    char buff[50]; 
    strcpy(result,""); 
    int i=0; 
    while(i<strlen(bytes)) 
    { 
     if(bytes[i]=='1') 
     { 

    a[0]=bytes[i]; 
    a[1]=bytes[i+1]; 
    a[2]=bytes[i+2]; 
    a[3]='\0'; 
    i=i+3; 
    } 
else 

{ 
    a[0]=bytes[i]; 
    a[1]=bytes[i+1]; 
    a[2]='\0'; 
    i=i+2; 
} 
ch = atoi(a); 
sprintf(buff,"%c",ch); 
strcat(result,buff); 
    } 
} 
/*this is the message that is going to be sent to the other server*/ 
char msg[MAXBUF]; 
/*this is the bytes recived*/ 
char bytes[MAXBUF]; 
void loadConfig(struct sockaddr_in *udpServer,struct sockaddr_in *thisServer,unsigned char *file); 

int sendDataToServerXX(struct sockaddr_in *udpServer, int udpSocket); 
int *useStdrr; 
int *maxRequests; 
int returnStatus; 

int main(int argc, char* argv[]) 
{ 
if (argc < 2) 
    { 
     fprintf(stderr, "Usage: %s <file.config adress>\n", argv[0]); 
     exit(1); 

    } 
useStdrr=malloc(sizeof(int)); 
maxRequests=malloc(sizeof(int)); 
    struct sockaddr_in udpServer,thisServer,udpClient; 
    loadConfig(&udpServer,&thisServer, argv[1]); 
    int thisServerSocket = createSocket(); 
    int udpSocket=createSocket(); 
int addrlen; 


printf("Listening on.. %d \n",thisServer.sin_port); 



thisServer.sin_family = AF_INET; 

returnStatus = bind(thisServerSocket, (struct sockaddr*)&thisServer, sizeof(thisServer)); 



if (returnStatus == 0) { 



    fprintf(stderr, "Bind completed!\n"); 



} 

else { 



    fprintf(stderr, "Could not bind to address \n"); 





    close(thisServerSocket); 

    exit(1); 

    } 



/*En este while infinito estamos esperando los datos de las tramas*/ 

while (1) 
    { 





    addrlen = sizeof(udpClient); 

    /* How to resive a struct? */ 

    returnStatus = recvfrom(thisServerSocket,(char*)&bytes, sizeof(bytes), 0, 

          (struct sockaddr*)&udpClient, &addrlen); 



     if (returnStatus == -1) { 



       fprintf(stderr, "Could not receive message!\n"); 





     } 





     else { 



     printf("Lo que llego: %s \n",bytes); 

     /*Primero quitamos el 0 y 1 y guardamos el nuevo arreglo en p*/ 



     bytes[strlen(bytes)-1]='\0'; 



     char p[strlen(bytes)]; 

     int i=0; 

     while(bytes[i+1]!='\0'){ 



      p[i]=bytes[i+1]; 

      i++; 

     } 





     /*esto simula la cambiada a base10 de base64*/ 

     char *result=malloc(512); 

     char *p2=malloc(sizeof(p)+1); 

     strcpy(p2,p); 

     cambiarAChars(p2,result); 



     strcat(result,"\n\0"); 

     printf("TAMANO: %d \n",strlen(result)); 

     char *output = unbase64(result, strlen(result)); 

     printf("Unbase64: %s\n", output); 



     msg[0]='%'; 

     strcat(msg,output); 
     int f=strlen(msg); 
     msg[f]='%'; 
     msg[f+1]='\0'; 

     printf("Voy a mandar: %s \n",msg); 

     sendDataToServerXX(&udpServer,udpSocket); 

     free(output); 




     } 







} 



close(thisServerSocket); 

    close(udpSocket); 

} 



int createSocket() 

{ 

/* create a socket */ 

int Socket; 

    Socket = socket(AF_INET, SOCK_DGRAM, 0); 



    if (Socket == -1) 

    { 

    if(*useStdrr) 

     { 

    fprintf(stderr, "Could not create a socket!\n"); 

    } 

    exit(1); 

    } 

    else { 

    printf("Socket created.\n"); 

    } 

return Socket; 

} 



void loadConfig(struct sockaddr_in *udpServer,struct sockaddr_in *thisServer, unsigned char *file) 

{ 

char line[256]; 

int linenum=0; 

    FILE* f = fopen(file, "r"); 

while(fgets(line, 256, f) != NULL) 

{ 

    char atribute[256], value[256]; 



    linenum++; 

    if(line[0] == '#'||line[0] == ' ') { 

    continue; 

    } 

    else{ 

    if(sscanf(line, "%s %s", atribute, value) != 2) 

    { 

      fprintf(stderr, "Syntax error, line %d\n", linenum); 

      continue; 

    } 



    if(!strcmp(atribute,"server_address")) 

    { 

     if(!strcmp(value,"")) 

     { 

     udpServer->sin_addr.s_addr = htonl(INADDR_ANY); 

     } 

     else{ 

     udpServer->sin_addr.s_addr = inet_addr(value); 

     } 

    } 

    else if(!strcmp(atribute,"server_port")) 

    { 

     udpServer->sin_port = htons(atoi(value)); 

    } 

    else if(!strcmp(atribute,"print_message_details")) 

    { 

     if(!strcmp(value,"ON")) 

     { 

      *useStdrr=1; 

     } 

     else 

     { 

      *useStdrr=0; 

     } 

    } 

    else if(!strcmp(atribute,"request_count")) 

    { 

     *maxRequests=5; 

    } 



    else if(!strcmp(atribute,"valor_que_viene_del_cohete_simulado")) 

    { 







    } 



    else if(!strcmp(atribute,"this_server_address")) 

    { 

     if(!strcmp(value,"")) 

     { 

      thisServer->sin_addr.s_addr = htonl(INADDR_ANY); 

     } 

     else{ 

      thisServer->sin_addr.s_addr = inet_addr(value); 

     } 

    } 

    else if(!strcmp(atribute,"this_server_port")) 

    { 

     thisServer->sin_port = htons(atoi(value)); 



    } 

} 



} 

} 



    int sendDataToServerXX(struct sockaddr_in *udpServer, int udpSocket) 

    { 
    udpServer->sin_family = AF_INET; 

int in=0; 

int boolv=0; 



while(in<*maxRequests) 

{ 

    in++; 



    returnStatus = sendto(udpSocket,(char*) &msg, sizeof(msg), 0, 

         (struct sockaddr*)udpServer, sizeof(*udpServer)); 



    if (returnStatus == -1) { 

     if(*useStdrr) 

     { 

     fprintf(stderr, "Could not send message!\n"); 



     } 

    } 

    else { 





     printf("Datos enviados al servidor xx.\n"); 

      memset(msg, 0, strlen(msg)); 

     in=*maxRequests; 

     boolv=1; 



    } 

} 

if(!boolv) 

{ 

    if(*useStdrr) 

    { 

    fprintf(stderr, "fixed number of requests finished.. no reply.\n"); 

    } 

} 

return 0; 
} 


char *unbase64(unsigned char *input, int length) 

{ 

    BIO *b64, *bmem; 
    char *buffer = (char *)malloc(length); 

    memset(buffer, 0, length); 



    b64 = BIO_new(BIO_f_base64()); 

    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); 

    bmem = BIO_new_mem_buf(input, length); 

    bmem = BIO_push(b64, bmem); 

    BIO_read(bmem, buffer, length); 
    BIO_free_all(bmem); 



return buffer; 

} 

好吧,所以我做了一个模拟器,发送数据到这台服务器......基本上是一个UDP客户端发送字节,因为我想要它们。连接和整个事情工作非常好:)。现在我试图连接到真正的测试者,这是一个java jar,它发送数据,因为我的服务器需要通过UDP。唯一的问题是,我没有Java源代码,因为它不是我的...但程序似乎运行顺利(Java jar)但是当我检查我的服务器没有连接的地方recived。是的,我正在等待正确的端口,并且C和Java都运行在同一台机器上(UBUNTU)。

我张贴用C做了我的客户端模拟器,工作得非常好的与此服务器。

对不起它有点长因为我从一个配置文件加载:

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netdb.h> 
    #include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 
#define MAXBUF 1024 
#define SENDING 0x52 
#define RESIVING 0xB4 
#define TYPE 0xA3F0 

int createSocket(); 


char msg[MAXBUF]; 





void loadConfig(struct sockaddr_in *udpServer, char *file); 

    int *useStdrr; 

    int *maxRequests; 

    int *timeOut; 



    int main(int argc, char* argv[]) 

{ 

    int returnStatus; 

    int addrlen; 

    struct sockaddr_in udpClient, udpServer; 

    char buf[MAXBUF]; 

useStdrr=malloc(sizeof(int)); 

maxRequests=malloc(sizeof(int)); 

timeOut=malloc(sizeof(int)); 

/*ms.timezone="AES";*/ 



    if (argc < 2) 

    {  

     fprintf(stderr, "Usage: %s <file.config adress>\n", argv[0]); 


    exit(1); 

    } 



int udpSocket=createSocket(); 

    udpServer.sin_family = AF_INET; 


loadConfig(&udpServer, argv[1]); 



/*how to send a struct here?*/ 

int in=0; 

int boolv=0; 

printf("Request number %i\n",*maxRequests); 

while(in<*maxRequests) 

{ 

    in++; 

    printf("Request number %i\n",in); 





    printf("Adresss::: %d\n",udpServer.sin_addr.s_addr); 

    printf("PORT:::: %i\n",udpServer.sin_port); 



    returnStatus = sendto(udpSocket,(char*) &msg, sizeof(msg), 0, 

         (struct sockaddr*)&udpServer, sizeof(udpServer)); 



    if (returnStatus == -1) { 

     if(*useStdrr) 

     { 

     fprintf(stderr, "Could not send message!\n"); 



     } 

    } 

    else { 



    printf("Message sent.\n"); 



    /* message sent: look for confirmation */ 

/* 

    addrlen = sizeof(udpServer); 



    returnStatus = recvfrom(udpSocket, (char*) &msg, sizeof(msg), 0, 

          (struct sockaddr*)&udpServer, &addrlen); 



    if (returnStatus == -1) { 

     if(*useStdrr) 

     { 

     fprintf(stderr, "Did not receive confirmation!\n"); 

     } 

    } 





    else { 



     printf("Second: %s\n", msg); 





     */ 

     in=*maxRequests; 

     boolv=1; 

     /* 







    }*/ 



    } 

} 

if(!boolv) 

{ 

    if(*useStdrr) 

    { 

    fprintf(stderr, "fixed number of requests finished.. no reply.\n"); 

    } 

} 





close(udpSocket); 

return 0; 



} 



int createSocket() 

{ 

/* create a socket */ 

int Socket; 

Socket = socket(AF_INET, SOCK_DGRAM, 0); 



if (Socket == -1) 

{ 

    if(*useStdrr) 

     { 

    fprintf(stderr, "Could not create a socket!\n"); 

    } 

    exit(1); 

} 

else { 

    printf("Socket created.\n"); 

} 

return Socket; 

} 



    void loadConfig(struct sockaddr_in *udpServer, char *file) 

    { 

    char line[256]; 

    int linenum=0; 




    FILE* f = fopen(file, "r"); 

while(fgets(line, 256, f) != NULL) 

{ 

    char atribute[256], value[256]; 



    linenum++; 

    if(line[0] == '#'||line[0] == ' ') { 

    continue; 

    } 

    else{ 

    if(sscanf(line, "%s %s", atribute, value) != 2) 

    { 

      fprintf(stderr, "Syntax error, line %d\n", linenum); 

      continue; 

    } 

    printf("Atribute: %s\n",atribute); 

    printf("Value: %s\n",value); 

    if(!strcmp(atribute,"server_address")) 

    { 

     if(!strcmp(value,"")) 

     { 

     udpServer->sin_addr.s_addr = htonl(INADDR_ANY); 

     } 

     else{ 

     udpServer->sin_addr.s_addr = inet_addr(value); 

     } 

    } 

    else if(!strcmp(atribute,"server_port")) 

    { 

     udpServer->sin_port = htons(atoi(value)); 

    } 

    else if(!strcmp(atribute,"print_message_details")) 

    { 

     if(!strcmp(value,"ON")) 

     { 

      *useStdrr=1; 

     } 

     else 

     { 

      *useStdrr=0; 

     } 

    } 

    else if(!strcmp(atribute,"request_count")) 

    { 

     *maxRequests=atoi(value); 

    } 

    else if(!strcmp(atribute,"request_*timeOut")) 

    { 

     *timeOut=atoi(value); 

    } 


} 



} 

} 

现在真正的问题:我必须做一些不同的连接弗罗马Java客户端,以从C服务器不是一个C是什么我确实要连接到另一个C客户端?如果答案是否定的,那么问题出现在java项目中?他们告诉我它的工作正常,但我认为他们已经用JAVA服务器测试过了。有没有区别?如果问题出现在java项目中,我应该告诉他们更改它以与我的C服务器一起工作?

很多thx!

亚历杭德罗·卡萨斯

+0

如果Java服务器通过网络堆栈发出UDP,那么你应该能够使用UDP客户端写以任何语言接收该数据。你有没有使用像wireshark这样的工具来验证数据包是从Java服务器发出的? – Chad 2012-03-12 19:00:43

+3

@Alejandro,如果你不打算花时间清理你的源码,那么很难指望其他用户花宝贵的时间来帮助你免费... – 2012-03-12 19:33:29

回答

1

有没有这样的事,作为一个UDP连接。在UDP中,一个程序只是在给定IP的给定端口上丢弃数据包。如果在该IP /端口处没有监听,或者接收程序决定忽略UDP数据包,则它将被丢弃。如果你想要一个实际的连接,你可以使用TCP。

此外,一些ISP块默认

在Java中某些类型的UDP数据包,TCP套接字称为套接字和UDP套接字被称为一个DatagramSocket。确保您从Java客户端的DatagramSocket发送。

我没有用C做套接字编程虽然。

最后,如果你发布你的一些Java代码,这将有助于。