c - is it possible to send a respose from udp server to client ? -


i posted code here: communication between windows client , linux server?

i performing communication between client , server.i know udp connectionless program nothing wont send response client. if want send response client should ?? solved errors in above link got doubt w.r.t sending response client. re posting here.

this code wrote when start learning socket programming, hope helps:

server

#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h>   int main(int argc, char *argv[]) {     struct sockaddr_in server;     struct sockaddr_in client;     int socket_fd;     int ret;     char buf[255];     int len = sizeof(struct sockaddr_in);      socket_fd = socket(af_inet, sock_dgram, 0);     if(socket_fd < 0)     {         printf("socket error\n");         return -1;     }      server.sin_family = af_inet;     server.sin_port = htons(5900);     server.sin_addr.s_addr = htonl(inaddr_any);      ret = bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr));     if(ret)     {         printf("error while binding\n");         return -1;     }      ret = recvfrom(socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client, &len);      if(ret < 0)     {         printf("reciving error\n");     }      printf("recving data %s: %s\n", inet_ntoa(client.sin_addr), buf);      snprintf(buf, sizeof(buf), "server:");     ret = sendto(socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&client, sizeof(client));      if(ret < 0)     {         printf("send error\n");         return -1;     }      close(socket_fd);      return 0; } 

client

#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h>  int main(int argc, char *argv[]) {     int socket_fd;     struct sockaddr_in server;     int ret;     char buf[255] = "send server";     int len = sizeof(struct sockaddr_in);      socket_fd = socket(af_inet, sock_dgram, 0);     if(socket_fd < 0)     {         printf("socket error\n");       }      server.sin_family = af_inet;     server.sin_port = htons(5900);     server.sin_addr.s_addr = inet_addr("127.0.0.1");      ret = sendto(socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&server, sizeof(server));      if(ret < 0)     {         printf("sendto error\n");         return -1;     }      ret = recvfrom(socket_fd, buf, sizeof(buf), 0, (struct sockaddr *)&server, &len);      if(ret < 0)     {         printf("error recv from\n");         return -1;     }     printf("recving server:%s: %s\n", inet_ntoa(server.sin_addr), buf);      close(socket_fd); } 

read code above , find answer question


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -