this is my udp server code

#include<iostream>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

void error( char *msg)
{
 perror(msg);
 exit(EXIT_FAILURE);
}
int main()
{
 int sockfd;
 sockfd = socket(AF_INET,SOCK_DGRAM,0);
 struct sockaddr_in serv,client;

 serv.sin_family = AF_INET;
 serv.sin_port = htons(53000);
 serv.sin_addr.s_addr = INADDR_ANY;

 char buffer[256];
 socklen_t l = sizeof(client);
 //socklen_t m = client;
 cout<<"\ngoing to recv\n";
 int rc= recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr *)&client,&l);
 if(rc<0)
 {
 cout<<"ERROR READING FROM SOCKET";
 }
 cout<<"\n the message received is : "<<buffer<<endl;
 int rp= sendto(sockfd,"hi",2,0,(struct sockaddr *)&client,l);

 if(rp<0)
 {
 cout<<"ERROR writing to SOCKET";
 }
}

this is my udp client code

#include<iostream>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

void error( char *msg)
{
 perror(msg);
 exit(EXIT_FAILURE);
}
int main()
{
 int sockfd;
 sockfd = socket(AF_INET,SOCK_DGRAM,0);
 struct sockaddr_in serv,client;

 serv.sin_family = AF_INET;
 serv.sin_port = htons(53000);
 serv.sin_addr.s_addr = inet_addr("127.0.0.1");

 char buffer[256];
 socklen_t l = sizeof(client);
 socklen_t m = sizeof(serv);
 //socklen_t m = client;
 cout<<"\ngoing to send\n";
 cout<<"\npls enter the mssg to be sent\n";
 fgets(buffer,256,stdin);
 sendto(sockfd,buffer,sizeof(buffer),0,(struct sockaddr *)&serv,m);
 recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&client,&l);
}

however these codes are not working and i since this is the first time i am doing socket programming so i cant put my finger on the reason.

any body who has an idea of whats the problem?

Your server code needs a bind statement to indicate what port its listening to.

bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.