Note that there are some explanatory texts on larger screens.

plurals
  1. POwinsock client and server communication
    primarykey
    data
    text
    <p>sorry I know I have been posting threads related to the same topic over throughout the past day but I've hit another issue.</p> <p>server.cpp</p> <pre><code>/*Server */ #define _WIN32_WINNT 0x501 #include &lt;iostream&gt; #include &lt;windows.h&gt; #include &lt;winsock2.h&gt; #include &lt;ws2tcpip.h&gt; using namespace std; const int winsock_version = 2; #define PORT "3490" #define MAX_NUM_CONNECTIONS 10 int main(void){ WSADATA wsadata; if (WSAStartup(MAKEWORD(winsock_version,0),&amp;wsadata) == 0){ cout&lt;&lt;"-WSAStartup Initialized." &lt;&lt; endl; struct addrinfo hints,*res; int sock_fd; memset(&amp;hints,0,sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if (getaddrinfo(NULL,PORT,&amp;hints,&amp;res) != 0){ cout&lt;&lt;"-Call to getaddress was unsucceful." &lt;&lt; endl; } if( (sock_fd = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol)) == -1){ cout&lt;&lt;"-Unable to Create socket." &lt;&lt; endl; } if ( (bind(sock_fd, res-&gt;ai_addr, res-&gt;ai_addrlen)) != -1 ){ cout&lt;&lt;"-binding successful." &lt;&lt; endl; } if ( (listen(sock_fd,MAX_NUM_CONNECTIONS)) != -1){ cout&lt;&lt;"-Listening for incoming connections." &lt;&lt; endl; } //------------------------------------------------------------- struct sockaddr_storage incming_info; socklen_t sin_size; sin_size = sizeof incming_info; int new_fd; new_fd = accept(sock_fd, (struct sockaddr*)&amp;incming_info,&amp;sin_size); if (new_fd == -1){ cout&lt;&lt;"-Accepting error." &lt;&lt; endl; } if(new_fd == INVALID_SOCKET){ cout&lt;&lt;"-INVALID SOCKET ERROR." &lt;&lt; endl; } //------------------------------------------------------------- cout&lt;&lt;"Connected?" &lt;&lt; endl; char buffer[128]; while(true){ int ret_val; ret_val = recv(new_fd,buffer,sizeof(buffer),0); if(ret_val == -1){ cout&lt;&lt;"Receiving Error." &lt;&lt; endl; break; }else if(ret_val == 0){ cout&lt;&lt;"Connection has been closed!." &lt;&lt; endl; break; }else if(ret_val &gt; 0){ cout&lt;&lt;"Server: " &lt;&lt; buffer&lt;&lt; endl; } } cout&lt;&lt;"-Closing connection" &lt;&lt; endl; closesocket(new_fd); }else{ cout&lt;&lt;"-WSAStartup Initialization failed." &lt;&lt; endl; if(WSACleanup()!=0){ cout&lt;&lt;"-WSACleanup Successful." &lt;&lt; endl; }else{ cout&lt;&lt;"-WSACleanup Failed." &lt;&lt; endl; } } return 0; } </code></pre> <p>client.cpp</p> <pre><code>/*client*/ #define _WIN32_WINNT 0x501 #include &lt;iostream&gt; #include &lt;windows.h&gt; #include &lt;winsock2.h&gt; #include &lt;ws2tcpip.h&gt; using namespace std; #define PORT "3490" #define SERVER "localhost" const int winsockVersion = 2; int main(void){ WSADATA wsadata; if ( (WSAStartup(MAKEWORD(2,0),&amp;wsadata)) == 0){ cout&lt;&lt;"-WSAStartup Initialized." &lt;&lt; endl; struct addrinfo hints, *res; int sockfd; memset(&amp;hints,0,sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(SERVER,PORT,&amp;hints,&amp;res) != 0){ cout&lt;&lt;"-getaddrinfo unsuccessful." &lt;&lt; endl; } if ( (sockfd = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol)) == -1 ){ cout&lt;&lt;"-Unable to create socket." &lt;&lt; endl; } if ( (connect(sockfd,res-&gt;ai_addr,res-&gt;ai_addrlen)) != -1 ){ cout&lt;&lt;"-Connection Established." &lt;&lt; endl; } cout&lt;&lt;"-Client connecting to: " &lt;&lt; res-&gt;ai_addr &lt;&lt; endl; while(true){ char text_buff[128]; cout&lt;&lt;"Enter text: "; cin&gt;&gt;text_buff; if( (send(sockfd,text_buff,sizeof(text_buff),0)) != -1 ){ cout&lt;&lt;"-text_buff sent!." &lt;&lt; endl; break; } closesocket(sockfd); } }else{ cout&lt;&lt;"-WSAStartup Initialization failed." &lt;&lt; endl; if(WSACleanup()!=0){ cout&lt;&lt;"-WSACleanup Successful." &lt;&lt; endl; }else{ cout&lt;&lt;"-WSACleanup Failed." &lt;&lt; endl; } } return 0; } </code></pre> <p>I can run the server and client fine, but I can only send one word (cant send a sentence for some reason) from the client which gets printed out on the server console but as soon as it is printed out on the server console it outputs "Receiving error" and closes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload