Hello everyone. I have to connect to a running server on another host [COMPLETED]. Then, I have to perform a webchat with any chosen single client in the server and I have some questions about that. I am able to connect to any chosen client in the server, I am able to send my messages through and receive something back. However,
Problems faced: The messages I receive and sent are not what is expected to be. For instance, I sent "Hello" to the receiver and he receives jibberish codes. When the receiver replies to me, I only receive his username in his reply but not his message.
I hope that made sense. I really need help to solve this problem.. Here are my necessary codes for this issue:
` #define MAX_MSG_LEN 1200 /* max size of any msg */
struct UDP{ /*to perform chat*/
char username[16];
char msg[100];
unsigned long userIP;
unsigned short portnumber;
} UDPchatee;
/************** UDP variables *************/
int UDP_socketID;
struct sockaddr_in serverAddr; //server details!
struct RegMsg *RegMessage; //registration message sent to server
int size = sizeof(serverAddr); //size of serverAddr
RegRespMsg_t RespMessage; //this is the response message received from server after UDP connection
struct in_addr userIP;
/****************************************/
/* variables used in both protocols */
int i; //the for-loop counter
int check; //this variable store the return values of sendto and receive functions to make sure
transmission is correct
/************ UDP variables for chat *****************/
char username[14];
int userfound; //flag that is set to 1 when user name type in exists
char message_sent[MAX_MSG_LEN];
int len; //message length
int UDP_socket;
struct sockaddr_in chateeAddr; //address of user you want to chat to
struct UDP *UdpChat;
int sizec = sizeof(chateeAddr);
char prompt[10]; //user response when asked to continue chatting
/*********************************************
//* UDP Chat */
//get the user name and IP you want to talk to
for (i=0; i < 16; i++)
username[i]= '\0';
printf("\nPlease type user name you want to talk to: ");
fgets(username, MAX_MSG_LEN, stdin);
strcpy(UDPchatee.username, username);
UDPchatee.username[strlen(UDPchatee.username)-1] = '\0'; //remove the '\n' character
userfound=-1;
for(i = 0; i< ntohl(RespMessage.nusers); i++)
{
if(strcmp(UDPchatee.username, RespMessage.user[i].username) == 0)
{
UDPchatee.userIP = RespMessage.user[i].ipAddr;
UDPchatee.portnumber = RespMessage.user[i].udpPort;
userfound = i;
}
}
if(userfound == -1){
printf("The username does not exist! Try again.\n\n");
continue;
}
//(1) Get a UDP socket //
UDP_socket = socket(AF_INET, SOCK_DGRAM, 0);
char msg[100], b1[100];
//(2) Store the information of server inside the chateeAddr structure,
bzero((char *)&chateeAddr,sizeof(chateeAddr));
chateeAddr.sin_family = AF_INET;
chateeAddr.sin_port = UDPchatee.portnumber; //get port number
chateeAddr.sin_addr.s_addr = UDPchatee.userIP; //get ip address
memset(chateeAddr.sin_zero, '\0', sizeof chateeAddr.sin_zero);
while(1)
{
//get message you want to send
printf("\nEnter a line of text as your message: ");
fgets(msg,MAX_MSG_LEN,stdin);
strcpy(UDPchatee.msg, msg);
check = sendto(UDP_socket,UdpChat,sizeof(UDPchatee),0,(struct sockaddr *)&chateeAddr,sizec);
if (check == -1)
printf("Sending is in error!\n\n");
check = recvfrom(UDP_socket,b1,sizeof(b1),0,(struct sockaddr *)&chateeAddr,&sizec);
if (check == -1)
printf("Receiving is in error\n");
printf("\nReply: %s",b1);
}`
I guess my main problem area is the last part where I'm not too sure what are the proper struct to input in the sendto() and recvfrom(). I hope it made sense! Thank you in advanced! :)