WINSOCK transmission in C++ loop -
i running server client winsock software transmit data in loop client server. there no problem in first transmission , perfect. second transmission , on corrupted , don't know if keep alive or else. spent 2 days trying figure out.
server side
#include"stdafx.h" #include <stdio.h> #include <stdlib.h> #include <winsock2.h> #include<windows.h> #include <time.h> #include"iostream" #include"string" #define maxline 1000 int main() { // initialize winsock wsadata wsadata; std::string message; int iresult = wsastartup(makeword(2, 2), &wsadata); if (iresult != no_error) printf("server: error @ wsastartup().\n"); // create socket listening incoming connection requests. socket socklisten; socklisten = socket(af_inet, sock_stream, 0); if (socklisten == invalid_socket) { printf("server: error @ socket(): %ld\n", wsagetlasterror()); wsacleanup(); return 0; } // sockaddr_in structure specifies address family, // ip address, , port socket being bound. struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof (servaddr)); servaddr.sin_family = af_inet; servaddr.sin_addr.s_addr = htonl(inaddr_any); servaddr.sin_port = htons(5000); /* daytime server */ if (bind(socklisten, (sockaddr*)&servaddr, sizeof(servaddr)) == socket_error) { printf("server: bind() failed.\n"); closesocket(socklisten); return 0; } // listen incoming connection requests on created socket if (listen(socklisten, 1) == socket_error) printf("server: listen(): error listening on socket.\n"); printf("server: i'm listening on socket, waiting connection...\n"); socket sockconn; char recvbuff[maxline]; while (1) { sockconn = accept(socklisten, null, null); recv(sockconn, recvbuff, maxline, 0); message = recvbuff; printf("%s \n", message); std::cout << wsagetlasterror(); sleep(100); memset(recvbuff, 0, maxline * (sizeof recvbuff[0])); } wsacleanup(); return 0; }
client side
#include"stdafx.h" #include <stdio.h> #include <stdlib.h> #include <winsock2.h> #include<windows.h> #include <time.h> #include"iostream" #define maxline 1000 int main() { // initialize winsock wsadata wsadata; int iresult = wsastartup(makeword(2, 2), &wsadata); if (iresult != no_error) printf("client: error @ wsastartup().\n"); // create socket connect server. socket sockclient; sockclient = socket(af_inet, sock_stream, 0); if (sockclient == invalid_socket) { printf("client: error @ socket(): %ld\n", wsagetlasterror()); wsacleanup(); return 0; } // sockaddr_in structure specifies address family, // ip address, , port socket being bound. struct sockaddr_in servaddr; char servhost[16]; memset(&servaddr, 0, sizeof (servaddr)); printf("enter host ip: "); scanf("%s", servhost); servaddr.sin_family = af_inet; servaddr.sin_addr.s_addr = inet_addr(servhost); servaddr.sin_port = htons(5000); /* daytime server */ // connect server. if (connect(sockclient, (sockaddr*)&servaddr, sizeof(servaddr)) == socket_error) { printf("client: connect() - failed connect.\n"); wsacleanup(); return 0; } char buff[maxline]; // read data server , display connect(sockclient, (sockaddr*)&servaddr, sizeof(servaddr)); (int x = 0; x < 100; x++) { sprintf(buff, "transmission number %d",x); send(sockclient, buff, strlen(buff), 0); memset(buff, 0, maxline * (sizeof buff[0])); sleep(3000); } closesocket(sockclient); wsacleanup(); closesocket(sockclient); return 0; }
the thing don't need loop
sockconn = accept(socklisten, null, null);
just put before server loop solved , problem solved not sure why
Comments
Post a Comment