EDN Admin
Well-known member
Hi all,<br/>
<br/>
i have created a basic console application for WinSock2 Client. Here is the code i use:
<div style="color:Black;background-color:White; <pre>
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
<span style="color:Green; // Link with ws2_32.lib
#pragma comment(lib, <span style="color:#A31515; "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT <span style="color:#A31515; "27015"
<span style="color:Blue; int __cdecl main() {
<span style="color:Green; //----------------------
<span style="color:Green; // Declare and initialize variables.
WSADATA wsaData;
<span style="color:Blue; int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
<span style="color:Blue; struct sockaddr_in clientService;
<span style="color:Blue; char *sendbuf = <span style="color:#A31515; "this is a test";
<span style="color:Blue; char recvbuf[DEFAULT_BUFLEN];
<span style="color:Blue; int recvbuflen = DEFAULT_BUFLEN;
<span style="color:Green; //----------------------
<span style="color:Green; // Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
<span style="color:Blue; if (iResult != NO_ERROR) {
printf(<span style="color:#A31515; "WSAStartup failed: %dn", iResult);
<span style="color:Blue; return 1;
}
<span style="color:Green; //----------------------
<span style="color:Green; // Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
<span style="color:Blue; if (ConnectSocket == INVALID_SOCKET) {
printf(<span style="color:#A31515; "Error at socket(): %ldn", WSAGetLastError() );
WSACleanup();
<span style="color:Blue; return 1;
}
<span style="color:Green; //----------------------
<span style="color:Green; // The sockaddr_in structure specifies the address family,
<span style="color:Green; // IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( <span style="color:#A31515; "127.0.0.1" );
clientService.sin_port = htons( 27015 );
<span style="color:Green; //----------------------
<span style="color:Green; // Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, <span style="color:Blue; sizeof(clientService) );
<span style="color:Blue; if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket);
printf(<span style="color:#A31515; "Unable to connect to server: %ldn", WSAGetLastError());
WSACleanup();
<span style="color:Blue; return 1;
}
<span style="color:Green; // Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (<span style="color:Blue; int)strlen(sendbuf), 0 );
<span style="color:Blue; if (iResult == SOCKET_ERROR) {
printf(<span style="color:#A31515; "send failed: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
<span style="color:Blue; return 1;
}
printf(<span style="color:#A31515; "Bytes Sent: %ldn", iResult);
<span style="color:Green; // Receive until the peer closes the connection
<span style="color:Blue; do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
<span style="color:Blue; if ( iResult > 0 )
printf(<span style="color:#A31515; "Bytes received: %dn", iResult);
<span style="color:Green; //I also print here the recvbuff just to read responce
<span style="color:Blue; else <span style="color:Blue; if ( iResult == 0 )
printf(<span style="color:#A31515; "Connection closedn");
<span style="color:Blue; else
printf(<span style="color:#A31515; "recv failed: %dn", WSAGetLastError());
} <span style="color:Blue; while( iResult > 0 );
<span style="color:Green; // cleanup
closesocket(ConnectSocket);
WSACleanup();
<span style="color:Blue; return 0;
}
[/code]
<br/>
My problem is that i recv some answer which is the login confirmation. I have to login with username and pass. So my msg is first the login request then n symbol then the msg which should subscribe me for data flow and last n. Seems the server there requires
n as command end separator. Then a data flow should start but it happens nothing. App just hangs so for internally.<br/>
<br/>
Is there something i miss here?<br/>
<br/>
Answers will be highly appreciated !<br/>
Thankx
View the full article
<br/>
i have created a basic console application for WinSock2 Client. Here is the code i use:
<div style="color:Black;background-color:White; <pre>
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
<span style="color:Green; // Link with ws2_32.lib
#pragma comment(lib, <span style="color:#A31515; "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT <span style="color:#A31515; "27015"
<span style="color:Blue; int __cdecl main() {
<span style="color:Green; //----------------------
<span style="color:Green; // Declare and initialize variables.
WSADATA wsaData;
<span style="color:Blue; int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
<span style="color:Blue; struct sockaddr_in clientService;
<span style="color:Blue; char *sendbuf = <span style="color:#A31515; "this is a test";
<span style="color:Blue; char recvbuf[DEFAULT_BUFLEN];
<span style="color:Blue; int recvbuflen = DEFAULT_BUFLEN;
<span style="color:Green; //----------------------
<span style="color:Green; // Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
<span style="color:Blue; if (iResult != NO_ERROR) {
printf(<span style="color:#A31515; "WSAStartup failed: %dn", iResult);
<span style="color:Blue; return 1;
}
<span style="color:Green; //----------------------
<span style="color:Green; // Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
<span style="color:Blue; if (ConnectSocket == INVALID_SOCKET) {
printf(<span style="color:#A31515; "Error at socket(): %ldn", WSAGetLastError() );
WSACleanup();
<span style="color:Blue; return 1;
}
<span style="color:Green; //----------------------
<span style="color:Green; // The sockaddr_in structure specifies the address family,
<span style="color:Green; // IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( <span style="color:#A31515; "127.0.0.1" );
clientService.sin_port = htons( 27015 );
<span style="color:Green; //----------------------
<span style="color:Green; // Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, <span style="color:Blue; sizeof(clientService) );
<span style="color:Blue; if ( iResult == SOCKET_ERROR) {
closesocket (ConnectSocket);
printf(<span style="color:#A31515; "Unable to connect to server: %ldn", WSAGetLastError());
WSACleanup();
<span style="color:Blue; return 1;
}
<span style="color:Green; // Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (<span style="color:Blue; int)strlen(sendbuf), 0 );
<span style="color:Blue; if (iResult == SOCKET_ERROR) {
printf(<span style="color:#A31515; "send failed: %dn", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
<span style="color:Blue; return 1;
}
printf(<span style="color:#A31515; "Bytes Sent: %ldn", iResult);
<span style="color:Green; // Receive until the peer closes the connection
<span style="color:Blue; do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
<span style="color:Blue; if ( iResult > 0 )
printf(<span style="color:#A31515; "Bytes received: %dn", iResult);
<span style="color:Green; //I also print here the recvbuff just to read responce
<span style="color:Blue; else <span style="color:Blue; if ( iResult == 0 )
printf(<span style="color:#A31515; "Connection closedn");
<span style="color:Blue; else
printf(<span style="color:#A31515; "recv failed: %dn", WSAGetLastError());
} <span style="color:Blue; while( iResult > 0 );
<span style="color:Green; // cleanup
closesocket(ConnectSocket);
WSACleanup();
<span style="color:Blue; return 0;
}
[/code]
<br/>
My problem is that i recv some answer which is the login confirmation. I have to login with username and pass. So my msg is first the login request then n symbol then the msg which should subscribe me for data flow and last n. Seems the server there requires
n as command end separator. Then a data flow should start but it happens nothing. App just hangs so for internally.<br/>
<br/>
Is there something i miss here?<br/>
<br/>
Answers will be highly appreciated !<br/>
Thankx
View the full article