473,379 Members | 1,335 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,379 software developers and data experts.

server not able to send data

34
hi i ve made my server but that is not able to send data ... can any one help me ...althhough the code is error free n the code is in c socket using winsock... n m using visuall c++
Apr 17 '08 #1
4 1481
Banfa
9,065 Expert Mod 8TB
I'm guessing it's a logic error in the code somewhere.

Have you checked that your server is actually attached to a network?
Apr 17 '08 #2
pralu
34
ya i have checked it ut .... its attached ... m giving u the code kindly help me ut.....


Expand|Select|Wrap|Line Numbers
  1. /***************************************************************************/
  2. /* This sample program provides a code for a connection oriented server.   */
  3. /***************************************************************************/
  4.  
  5.  
  6. /***************************************************************************/
  7. /* Header files needed for the program.                                       */
  8. /***************************************************************************/
  9. #include "structure.h"
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <Winsock2.h>
  13. #include <Ws2tcpip.h>
  14. #include <stdlib.h>
  15. #include <string.h>        
  16.  
  17.  
  18. /***************************************************************************/
  19. /* Constants used by the program.                                           */
  20. /***************************************************************************/
  21. #define SERVER_PORT        3005
  22. #define FALSE               0
  23. #define EVENT_LOG_ENTRY  100 
  24.  
  25.  
  26. void main()
  27. {
  28.     /***************************************************************/
  29.     /* Variables and structure definations                           */
  30.     /***************************************************************/
  31.     int sd=-1,sd2=-1;
  32.     int rc,on=1;
  33.     fd_set read_fd;
  34.     struct timeval timeout;
  35.     struct sockaddr_in serveraddr;
  36.  
  37.  
  38.  
  39.     /****************************************************************/
  40.     /* A do/while(False) loop is used to make error cleaner easier. */
  41.     /* The close() of each of the socket descriptor is done at the  */
  42.     /* end of the program.                                            */
  43.     /****************************************************************/
  44.     do
  45.     {
  46.  
  47.         /************************************************************/
  48.         /*    call WSAStartup to startup the interface to WinSock.    */
  49.         /************************************************************/
  50.         WSADATA w;
  51.         if (WSAStartup(0x0101, &w) != 0)
  52.         {
  53.             fprintf(stderr, "Could not open Windows socket connection.\n");
  54.             exit(0);
  55.         }
  56.  
  57.         /**************** WinSock has been initialized ******************/
  58.  
  59.         /****************************************************************/
  60.         /* A socket() function returns the socket descriptor represent- */
  61.         /* ing an endpoint.The statement also identifies the INET (In-  */
  62.         /* ternet protocol) address family with the TCP transport (SOCK_*/
  63.         /* STREAM) will be used for this socket.                        */
  64.         /****************************************************************/
  65.         sd = socket(PF_INET, SOCK_STREAM, 0);
  66.         if(sd < 0)
  67.         {
  68.             perror("socket() failed");
  69.             break;
  70.         }
  71.  
  72.  
  73.         /****************************************************************/
  74.         /* The setsockopt() function is used to allow the local address */
  75.         /* to be reused when the server is restarted before the required*/
  76.         /* wait time expires.                                            */
  77.         /****************************************************************
  78.         /*rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)on, sizeof(on));
  79.         if(rc < 0)
  80.         {
  81.             perror("setsockopt(SO_REUSEADDR) failed");
  82.             break;
  83.         }*/
  84.  
  85.  
  86.         /****************************************************************/
  87.         /* After the socket descriptor is created, a bind() gets a uniq-*/
  88.         /* ue name for the socket. In this example the user sets the    */
  89.         /* s_addr to zero, which allows connections to be established    */
  90.         /* from any client that specifies port no 3005                    */
  91.         /****************************************************************/
  92.         memset(&serveraddr, 0 , sizeof(serveraddr));
  93.         serveraddr.sin_family        =    PF_INET;
  94.         serveraddr.sin_port            =    htons(SERVER_PORT);
  95.         serveraddr.sin_addr.s_addr  =   INADDR_ANY;
  96.  
  97.         rc = bind(sd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
  98.         if(rc < 0)
  99.         {
  100.             perror("bind() failed");
  101.             break;
  102.         }
  103.  
  104.  
  105.         /****************************************************************/
  106.         /*  The listen() allows the server to accept incoming client    */
  107.         /*    connections. In this the backlog is set to 10, this means    */
  108.         /*  that the system will queue 10 incoming connection requests    */
  109.         /****************************************************************/
  110.         rc = listen(sd,10);
  111.         if(rc < 0)
  112.         {
  113.             perror("listen() failed");
  114.             break;
  115.         }
  116.  
  117.  
  118.         printf("ready for the client connect() \n");\
  119.  
  120.         /****************************************************************/
  121.         /* Server uses the accept() to accept the incoming connection    */
  122.         /* request. the accept() call will block the indefinitely wait- */
  123.         /* ing for incoming connections to arrive.                        */
  124.         /****************************************************************/
  125.         sd2 = accept(sd, NULL, NULL);
  126.         if(sd2 < 0)
  127.         {
  128.             perror("accept() failed");
  129.             break;
  130.         }
  131.  
  132.  
  133.         /****************************************************************/
  134.         /* The select() allows the process to wait for an event to occur*/
  135.         /* and wake up the process when the event occurs. In this the    */
  136.         /* system notifies the process only when it has data available    */
  137.         /* to read. The 30 seconds timeout is used on this select call  */ 
  138.         /****************************************************************/
  139.         timeout.tv_sec  = 30;
  140.         timeout.tv_usec =  0;
  141.  
  142.         FD_ZERO(&read_fd);
  143.         FD_SET(sd2, &read_fd);
  144.  
  145.         rc = select(sd2+1, &read_fd, NULL, NULL, &timeout);
  146.         if(rc < 0)
  147.         {
  148.             perror("select() failed");
  149.             break;
  150.         }
  151.  
  152.         if(rc == 0)
  153.         {
  154.             perror("select() time out.");
  155.             break;
  156.         }
  157.  
  158.  
  159.  
  160.         /*****************************************************************/
  161.         /*    Data entry that will be passed to the client                 */
  162.         /*****************************************************************/
  163.  
  164.         EventLogEntryStruct EventLogEntry[EVENT_LOG_ENTRY];
  165.  
  166.         char buffer[20] = "";
  167.  
  168.         for(int i=0;i<10;i++) // this will reset after the max val is reached
  169.         {
  170.  
  171.             strcpy(EventLogEntry[i].logName,"log-");
  172.             strcpy(EventLogEntry[i].eventString,"event-");
  173.  
  174.             EventLogEntry[i].bufPos = i;
  175.             EventLogEntry[i].logIdx = i+1;
  176.             itoa (i,buffer,10);
  177.             strcat(EventLogEntry[i].logName,buffer);
  178.             EventLogEntry[i].dateTime = i+4;
  179.             EventLogEntry[i].systemTick = i;
  180.             EventLogEntry[i].resetCount = 1;
  181.             EventLogEntry[i].inAir = 1;
  182.             EventLogEntry[i].appSpec1 = i+2;
  183.             EventLogEntry[i].appSpec2 = i+3;
  184.             EventLogEntry[i].appSpec3 = i+1;
  185.             EventLogEntry[i].timeStampLSW = 12;
  186.             EventLogEntry[i].timeStampMSW = 24;
  187.             EventLogEntry[i].timeStamp = 22;
  188.             EventLogEntry[i].timeDelta = 12;
  189.             EventLogEntry[i].windowID = i+2;
  190.             strcat(EventLogEntry[i].eventString,buffer);
  191.  
  192.  
  193.             /*******************************************************************/
  194.             /* Echo the data back to the client                                   */
  195.             /*******************************************************************/
  196.             rc = send(sd2, (char *)&EventLogEntry[i], sizeof(EventLogEntryStruct), 0);
  197.             if(rc < 0)
  198.             {
  199.                 printf("send() failed");
  200.                 break;
  201.             }
  202.         }
  203.  
  204.  
  205.  
  206.     /*******************************************************************/
  207.     /* Program complete                                                   */
  208.     /*******************************************************************/
  209.     }while(false);
  210.  
  211.  
  212.     /*******************************************************************/
  213.     /* Close down any open socket                                       */
  214.     /*******************************************************************/
  215.     if(sd != -1)
  216.         closesocket(sd);
  217.     if(sd2 != -1)
  218.         closesocket(sd2);
  219. }
Apr 17 '08 #3
Banfa
9,065 Expert Mod 8TB
What error number do you get when you try to send?
Apr 17 '08 #4
pralu
34
select() time out on both server n client side which i ve handeled in...
Apr 18 '08 #5

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

Similar topics

15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
1
by: Novice | last post by:
Hey all, I'm trying to write a proxy server so that I can capture all data sent from my web browser to any web server and then capture the response from the server and send that back to the...
6
by: FS Liu | last post by:
Hi, In my current ATL server project, I have to parse the input in the client application's request to find out the different combination of parameters, instead of using SOAP. For this reason,...
9
by: Steve Buster | last post by:
All right, I have read every forum, newsgroup etc about this issue and no one seems to know how to fix it. I am getting a "Server Application Unavailable" exception running my .NET 1.1...
1
by: Mike Dole | last post by:
I'm sorry to bother you with this question but it was either this or giving up and trying to go for a simpler solution (which I will if this is not gonna work out..) I'm afraid this is way out...
7
by: Ole | last post by:
Hi, I'm going to develop a socket communication between an instrument (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only connect to one PC at a time I believe that...
1
by: Prasanta | last post by:
Hello, Please cnay one can tell me how to read mail as formatted.... i have made some code using that able to read but not able to serialize..... so am i need to parse the HTML, or is there any...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
7
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize,...
4
by: Babloo | last post by:
Hi everyone, i wanted to implement a client- server connection and transfer a file over the network. i was able to implement a normal set up where in i was able to send chat messages and small...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.