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++
server not able to send data
Collapse
X
-
ya i have checked it ut .... its attached ... m giving u the code kindly help me ut.....
Code:/***************************************************************************/ /* This sample program provides a code for a connection oriented server. */ /***************************************************************************/ /***************************************************************************/ /* Header files needed for the program. */ /***************************************************************************/ #include "structure.h" #include <stdio.h> #include <sys/types.h> #include <Winsock2.h> #include <Ws2tcpip.h> #include <stdlib.h> #include <string.h> /***************************************************************************/ /* Constants used by the program. */ /***************************************************************************/ #define SERVER_PORT 3005 #define FALSE 0 #define EVENT_LOG_ENTRY 100 void main() { /***************************************************************/ /* Variables and structure definations */ /***************************************************************/ int sd=-1,sd2=-1; int rc,on=1; fd_set read_fd; struct timeval timeout; struct sockaddr_in serveraddr; /****************************************************************/ /* A do/while(False) loop is used to make error cleaner easier. */ /* The close() of each of the socket descriptor is done at the */ /* end of the program. */ /****************************************************************/ do { /************************************************************/ /* call WSAStartup to startup the interface to WinSock. */ /************************************************************/ WSADATA w; if (WSAStartup(0x0101, &w) != 0) { fprintf(stderr, "Could not open Windows socket connection.\n"); exit(0); } /**************** WinSock has been initialized ******************/ /****************************************************************/ /* A socket() function returns the socket descriptor represent- */ /* ing an endpoint.The statement also identifies the INET (In- */ /* ternet protocol) address family with the TCP transport (SOCK_*/ /* STREAM) will be used for this socket. */ /****************************************************************/ sd = socket(PF_INET, SOCK_STREAM, 0); if(sd < 0) { perror("socket() failed"); break; } /****************************************************************/ /* The setsockopt() function is used to allow the local address */ /* to be reused when the server is restarted before the required*/ /* wait time expires. */ /**************************************************************** /*rc = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)on, sizeof(on)); if(rc < 0) { perror("setsockopt(SO_REUSEADDR) failed"); break; }*/ /****************************************************************/ /* After the socket descriptor is created, a bind() gets a uniq-*/ /* ue name for the socket. In this example the user sets the */ /* s_addr to zero, which allows connections to be established */ /* from any client that specifies port no 3005 */ /****************************************************************/ memset(&serveraddr, 0 , sizeof(serveraddr)); serveraddr.sin_family = PF_INET; serveraddr.sin_port = htons(SERVER_PORT); serveraddr.sin_addr.s_addr = INADDR_ANY; rc = bind(sd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)); if(rc < 0) { perror("bind() failed"); break; } /****************************************************************/ /* The listen() allows the server to accept incoming client */ /* connections. In this the backlog is set to 10, this means */ /* that the system will queue 10 incoming connection requests */ /****************************************************************/ rc = listen(sd,10); if(rc < 0) { perror("listen() failed"); break; } printf("ready for the client connect() \n");\ /****************************************************************/ /* Server uses the accept() to accept the incoming connection */ /* request. the accept() call will block the indefinitely wait- */ /* ing for incoming connections to arrive. */ /****************************************************************/ sd2 = accept(sd, NULL, NULL); if(sd2 < 0) { perror("accept() failed"); break; } /****************************************************************/ /* The select() allows the process to wait for an event to occur*/ /* and wake up the process when the event occurs. In this the */ /* system notifies the process only when it has data available */ /* to read. The 30 seconds timeout is used on this select call */ /****************************************************************/ timeout.tv_sec = 30; timeout.tv_usec = 0; FD_ZERO(&read_fd); FD_SET(sd2, &read_fd); rc = select(sd2+1, &read_fd, NULL, NULL, &timeout); if(rc < 0) { perror("select() failed"); break; } if(rc == 0) { perror("select() time out."); break; } /*****************************************************************/ /* Data entry that will be passed to the client */ /*****************************************************************/ EventLogEntryStruct EventLogEntry[EVENT_LOG_ENTRY]; char buffer[20] = ""; for(int i=0;i<10;i++) // this will reset after the max val is reached { strcpy(EventLogEntry[i].logName,"log-"); strcpy(EventLogEntry[i].eventString,"event-"); EventLogEntry[i].bufPos = i; EventLogEntry[i].logIdx = i+1; itoa (i,buffer,10); strcat(EventLogEntry[i].logName,buffer); EventLogEntry[i].dateTime = i+4; EventLogEntry[i].systemTick = i; EventLogEntry[i].resetCount = 1; EventLogEntry[i].inAir = 1; EventLogEntry[i].appSpec1 = i+2; EventLogEntry[i].appSpec2 = i+3; EventLogEntry[i].appSpec3 = i+1; EventLogEntry[i].timeStampLSW = 12; EventLogEntry[i].timeStampMSW = 24; EventLogEntry[i].timeStamp = 22; EventLogEntry[i].timeDelta = 12; EventLogEntry[i].windowID = i+2; strcat(EventLogEntry[i].eventString,buffer); /*******************************************************************/ /* Echo the data back to the client */ /*******************************************************************/ rc = send(sd2, (char *)&EventLogEntry[i], sizeof(EventLogEntryStruct), 0); if(rc < 0) { printf("send() failed"); break; } } /*******************************************************************/ /* Program complete */ /*******************************************************************/ }while(false); /*******************************************************************/ /* Close down any open socket */ /*******************************************************************/ if(sd != -1) closesocket(sd); if(sd2 != -1) closesocket(sd2); }
Comment
Comment