473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Message from udp server can't reach udp client

Expand|Select|Wrap|Line Numbers
  1. Client code:-
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <netdb.h> 
  7. #include <stdio.h>
  8. #include <sys/unistd.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <strings.h>
  12.  
  13.  
  14.  
  15. #define STRLEN 1024
  16.  
  17. int main(int argc, char *argv[]) {
  18.  
  19.   int sockfd, n, slen;
  20.  
  21.   char sendline[STRLEN], recvline[STRLEN];
  22.   struct sockaddr_in servaddr;
  23.   int gethostname(char *name, size_t len);
  24.   struct hostent *h;
  25.   char hostname[128];
  26.   struct sockaddr_in sa;
  27.  
  28.    if(argc != 3) 
  29.   {
  30.     printf("Usage: %s <Enter Server Address> <Enter port no >\n", argv[0]);
  31.     exit(0);
  32.   }
  33.  
  34.  
  35.    gethostname(hostname, sizeof hostname);
  36.    printf("\n\nYou are connected from:- %s\n", hostname);
  37.  
  38.   /* create hostent structure from  user entered host name*/
  39.   if ( (h = gethostbyname(argv[1])) == NULL)
  40.   {
  41.     printf("\n%s: error in gethostbyname()", argv[0]);
  42.     exit(0);
  43.   }
  44.  
  45.  
  46.   /* create server address structure */
  47.   bzero(&servaddr, sizeof(servaddr)); /* initialize it */
  48.   servaddr.sin_family = AF_INET;
  49.   servaddr.sin_addr.s_addr = htonl(inet_addr(argv[1]));
  50.   servaddr.sin_port = htons(atoi(argv[2])); /* get the port number from argv[2]*/
  51.  
  52.  if ( (sockfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0) 
  53.  
  54.   {
  55.     printf("\n%s: error in socket()", argv[0]);
  56.     exit(0);
  57.   }
  58.  
  59.  
  60.  
  61.   printf("\nEnter The name of the file you want to search:- ");
  62.   scanf("%s",sendline);
  63.  
  64.   if (sendto(sockfd, sendline, sizeof(sendline), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) 
  65.      {
  66.       printf("\n%s: error in sendto()", argv[0]);
  67.       exit(0);
  68.  
  69.       }
  70.  
  71.   slen = sizeof(servaddr);
  72.   if ( (n = recvfrom(sockfd, recvline, STRLEN, 0, (struct sockaddr *) &servaddr, &slen)) < 0) {
  73.       printf("\n%s: error in recvfrom()", argv[0]);
  74.       exit(0);
  75.   }
  76.  
  77.  recvline[n] = 0;
  78.  
  79.  
  80.   fputs(recvline, stdout);
  81.  
  82.   close(sockfd);
  83.  
  84. }
  85.  
  86.  
  87.  
  88. Server Code:-
  89.  
  90. #include <sys/types.h>
  91. #include <sys/socket.h>
  92. #include <netinet/in.h>
  93. #include <arpa/inet.h>
  94. #include <netdb.h>
  95. #include <stdio.h>
  96. #include <stdlib.h>
  97. #include <strings.h>
  98. #include <sys/stat.h>
  99. #include <fcntl.h>
  100.  
  101. #define STRLEN 1024
  102.  
  103. int main(int argc, char *argv[]) {
  104.  
  105.   int sockfd, clen, n;
  106.   struct sockaddr_in servaddr, cliaddr; 
  107.   struct stat buffer;
  108.   int status;
  109.   char a[50];
  110.   struct stat m;
  111.  
  112.  
  113.   char str_in[STRLEN];
  114.  
  115.   if (argc != 2) {
  116.     printf("\nUsage: %s < Enter port no>", argv[0]);
  117.     exit(0);
  118.   }
  119.  
  120.  
  121.   /* socket creation */
  122.   if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  123.     printf("\n%s: error in socket()", argv[0]);
  124.     exit(0);
  125.   }
  126.  
  127.   /* create local server socket structure */
  128.   bzero(&servaddr, sizeof(servaddr));
  129.   servaddr.sin_family = AF_INET;
  130.   servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
  131.   servaddr.sin_port = htons(atoi(argv[1]));
  132.  
  133.   /* bind it to an address */
  134.   if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))< 0)
  135.  {
  136.     printf("\n%s: error in bind()", argv[0]);
  137.     exit(0);
  138.   }
  139.  
  140.    clen = sizeof(cliaddr);
  141.   if ( (n = recvfrom(sockfd, str_in, STRLEN, 0, (struct sockaddr *) &cliaddr, &clen)) < 0) {
  142.     printf("\n%s: error in recvfrom()", argv[0]);
  143.     exit(0);
  144.   }
  145. printf("%s\n",str_in);
  146.  
  147.    sprintf (a,"data/%s", str_in);
  148.    printf("%s\n",a);
  149.    status= stat( a, &buffer );
  150.  
  151.    if (status ==0)
  152.      {
  153.        printf("File found");
  154.        sprintf(str_in,"%s",buffer.st_size);
  155.      }
  156. else
  157.      {
  158.        printf("File not found");
  159.        sprintf(str_in,"0");
  160.      }
  161.  
  162. if (sendto(sockfd,str_in,n, 0, (struct sockaddr *) &cliaddr, clen) < 0) {
  163.     printf("\n%s: error in sendto()", argv[0]);
  164.     exit(0);
  165.   }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.   close(sockfd);
  172.  
  173. }
  174.  
  175.  
Sep 24 '10 #1
1 1685
weaknessforcats
9,208 Expert Mod 8TB
Have you stepped through this using your debugger?

It's what I would have to do.
Sep 25 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Thomas | last post by:
Hallo. Habe auf einem SeSE 9.1 Server due neue MySQL 4.1.11 und den dazugehörigen client installiert. #: mysql -V mysql Ver 14.7 Distrib 4.1.11, for pc-linux-gnu (i686) Soweit so gut der...
17
by: Zvi Danovich | last post by:
Hi, I am a newby in ASP.NET, and till now wrote only simple "classic" WEB-sites. But - the time came, and now I need that: 1. Server will "listen" for some events on its local machine 2....
1
by: Lau Lei Cheong | last post by:
I have an aspx page which have the following code segment: <script> // I do something here template = "<input id=\"hidden_info_1\" type=\"hidden\" runat=\"server\"> // I do other things here...
1
by: vidya | last post by:
Hi, I have a button which is a web control. I have some validation in javascript for the button in .aspx file and some in the button onclick event in code behind(C#). I need to get through both...
9
by: jacob | last post by:
Hi everyone: I have a problem in ASP NET. Here is my situation. I developed an ASP NET application. It concerns data statistics. DB is oracle 10g, Web server is IIS 6.0, Server OS is Win2003,...
1
by: drk.kumar | last post by:
I have an implementation issue with WMI scripts to check the user machine processor. The implementation is working fine in the local machine (Windows XP operating system). It is throwing script...
2
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes...
5
by: Vili | last post by:
Hi all I am having a problem here Is it possible to fire a server side function from client side? ie. I have a function on codebehind Sub DoSomething(o as object)
1
by: haytham2008 | last post by:
can i reach file using any client script ?? eg. javascript or any other language and i have access to read file without the user allow me please i need to know about this thing :$
5
by: Simon | last post by:
I heard that we could do that by using AJAX. Could anybody share how to do it? Thanks.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.