다른 질문이 있습니다. C에서 간단한 바이트 스트링을 읽는 간단한 UDP 서버가 있습니다. 스트링이 ### @ ## ## @ ### @ ### 그는 C를 통해 다른 서버에 UDP를 보냅니다. 다음은 preprocesamiento.c라고하는 C 서버의 코드입니다. 전체를 게시하는 것은 쉬운 일이지만 어쩌면 여기에는 아무 것도 없습니다. 내 문제와.C 서버와 Java 클라이언트 간의 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 클라이언트. 그리고 연결과 모든 일이 아주 좋은 :). 이제 실제 테스터에 연결하려고합니다. 이는 내 서버가 UDP를 통해 원하는대로 데이터를 보내는 Java jar입니다. 유일한 문제는 Java 소스 코드가 없다는 것입니다. 왜냐하면 그 프로그램이 원활하지 않기 때문입니다.하지만 프로그램이 부드럽게 실행되는 것처럼 보입니다. 그리고 네, 올바른 포트에서 대기하고 두 프로그램 모두 C와 Java가 동일한 시스템 (UBUNTU)에서 실행 중입니다.
나는이 서버와 잘 작동하는 C로 만든 클라이언트 시뮬레이터를 게시한다.
죄송는 내가 config 파일에서로드 조금 긴 COS :
#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);
}
}
}
}
이제
실제 질문 :보다 C 서버에서 C로 froma 자바 클라이언트를 연결 뭔가 다른 일을해야합니까 무엇 다른 C 클라이언트에 연결합니까? 대답이 아니오라면 자바 프로젝트에 문제가 있습니까? 그들은 잘 작동하지만 자바 서버로 테스트 한 것 같아요. 어떤 차이가 있습니까? 문제가 Java 프로젝트에 있다면 C 프로젝트에서 변경하도록 말해야합니까?
많은 thx !!!
알레한드로 카사스는
만약 자바 서버가 네트워크 스택을 통해 UDP를 보내고는, 당신은 UDP 클라이언트가 작성 사용할 수 있어야합니다 모든 언어로 해당 데이터를 수신 할 수 있습니다.Wireshark와 같은 도구를 사용하여 패킷이 Java 서버에서 나오는지 확인 했습니까? – Chad
@Alejandro, 시간을내어 소스를 정리하지 않으면 다른 사용자가 무료로 도움을 줄 수있는 귀중한 시간을 가질 것으로 기대하는 것이 어렵습니다. –