trouble sending messages to client and server

hi guys, i`m having trouble sending messages from my server to a client.
I`m doing a udp client server program.

what i`m trying to do is to send a message to the client
However, it doesn`t seem that it is sending
I created a dummy msg like hello world to be sent over. However, it seems that my sendto function is not functioning as intended.

Below are the code snippets

Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 while(1)
    {
        if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_addr, &slen)==-1)
	   {
            err("recvfrom()");
	   }
        printf("Received packet from %s:%d\nData: %s\n\n",
               inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buf);
	strcpy(msg,"hello world");

	printf("message to send is %s",msg);
	if(sendto(sockfd,msg,BUFLEN,0,(struct sockaddr*)&cli_addr,&slen)==-1)
	{
	  err("sendto()");
	}
 }


Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 while(1)
    {
        printf("\nEnter data to send(Type exit and press enter to exit) : ");
        scanf("%[^\n]",buf);
        getchar();
        if(strcmp(buf,"exit") == 0)
        {
          exit(0);
	}
        if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, &slen)==-1)
	{
            err("sendto()");
	}

	recvfrom(sockfd,msg,BUFLEN,0,(struct sockaddr*)&serv_addr,&slen);
	printf("msg received from server %s",msg);
 }


Please tell me where the code went wrong. Thanks in advance!
Last edited on
Topic archived. No new replies allowed.