hello all,

i have simple udp server program as followed.

Code:
#cat ex_4.c 
/*a UDP serve program. This program creates a socket and binds to a port address given by the user. Now before reading the socket for any message from client, it takes the number of bytes to read, from the user.Then it uses the read() system call to read the message from the client.The third argument to read() is the number of bytes specified by the user.After reading any message , it simply displays the message and again asks the user to give size of message.This continues in a forever loop. 
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

#define MAX_MSG 100

void toggleCase(char* mesg,int cnt);

void errExit(char *str)
{
     puts(str);
     exit(0);
}

int main()
{
     int sockFd;
     struct sockaddr_in srvAdr, cliAdr;
     int cliLen,n;
     int portNo,noOfBytes;
     char mesg[MAX_MSG];

     printf("Enter the server port number \n");
     scanf("%d",&portNo);

     if((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	  errExit("Can't open datagram socket\n");

     memset(&srvAdr, 0, sizeof(srvAdr));
     srvAdr.sin_family = AF_INET;
     srvAdr.sin_addr.s_addr = htonl(INADDR_ANY);
     srvAdr.sin_port = htons(portNo);

     if(bind(sockFd,(struct sockaddr*)&srvAdr, sizeof(srvAdr)) < 0)
	  errExit("Can't bind local address \n");

     printf("Server waiting for messages \n");

     while(1)
     {
	  printf("Enter number of bytes to read from the client.\n");
	  scanf("%d",&noOfBytes);
	  
	  cliLen = sizeof(cliAdr);

	  n = read(sockFd, &mesg, noOfBytes);

	  mesg(noOfBytes+1) = '\0';

	  if(n < 0)
	       errExit("recv from");
	  
	  toggleCase(mesg,n);
	  
	  printf("Received following message from client %s\n",mesg);
     }

     close(sockFd);
}


void toggleCase(char *buf,int cnt)
{
     int ii;
     
     for(ii=0;ii<cnt;ii++)
     {
	  if(buf[ii] >= 'A' && buf[ii] <= 'Z')
	       buf[ii] += 0x20;
	  else if(buf[ii] >= 'a' && buf[ii] <= 'z')
	       buf[ii] -= 0x20;
     }
}
Here when i read number of bytes using read i even see some junk message on my console as
below along with the message which i sent . i you see i get junk after "J".

output
--------

./ex4
Enter the server port number
8000
Server waiting for messages
Enter number of bytes to read from the client.
10
Received following message from client ABCDEFGHIJ��܁T\��\ڜ



why am i seeing this

iam using following as my udp client to send messages.

Code:
$ cat ex_3.c
/* 
UDP client program developed in exercise 2 has some limitation.If server program does not send a response .The client program will block and never exits.So with the help of select() seystem implement a timeout feature(You may do this exercise only after select() system call is covered in the class).
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <curses.h>

#define MAX_MSG 100
#define TIMEOUT 5

void errExit(char *str)
{
     puts(str);
     exit(0);
}

int main()
{
     int sockFd;
     struct timeval tv;
     struct sockaddr_in srvAdr;
     char txmsg[MAX_MSG];
     char rxmsg[MAX_MSG];
     char serverIpAddress[32];
     int serverPortNo = 0;
     fd_set readfds;
     int n;
     int ret;
     
     memset(serverIpAddress,0,sizeof(serverIpAddress));

     if((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	  errExit("Can't open datagram socket\n");

     printf("Enter the server ip address and port no");
     scanf("%s %d\n",serverIpAddress,&serverPortNo);

     memset(&srvAdr, 0, sizeof(srvAdr));
     srvAdr.sin_family = AF_INET;
     srvAdr.sin_addr.s_addr = inet_addr(serverIpAddress);
     srvAdr.sin_port = htons(serverPortNo);

     fflush(stdin);
     printf("Enter message to send\n");
     fgets(txmsg,MAX_MSG,stdin);

     n = strlen(txmsg) + 1;

     if(sendto(sockFd,txmsg,n,0,(struct sockaddr *)&srvAdr,sizeof(srvAdr)) != n)
	  errExit("send to error\n");


     close(sockFd);
}

input
----
./ex3
Enter the server ip address and port no127.0.0.1 8000
abcdefghijklmnopqrst
Enter message to send



here another dobut , even though i kept a fflush on stdin to print "enter message to send" either it takes "\n" as input or it waits to get it printed after i enter the message as you can see in my input.

please let me know your comments where am i missing .