Using Non Blocking socket in a port scanner - c

I have written an simple port scanner code to see the whether the host is available or not by establishing the socket connection .The logic is to check for connection if the connection established is successful then the host is available or else if we get error code as WSACONNREFUSED then the host is present.The socket connection is a blocking one and if there's no host on that ip address the program will be blocked until an timeout occurs . I read a couple of examples on the internet on how to use a non blocking socket but they use read and write to check whether we can write to server side or read from the server side to establish the connection .and for that there should be a server code to accept.Is there a way to modify the existing code for the port scanner using a non blocking socket,as i want the present application to run a bit faster any valuable tips would be useful .
The present code is below .
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "ws2_32.lib")
int port[]={80,139};
int wmain()
{
// Initialize Winsock
WSADATA wsaData;
int i=0,flag=0;
char ip[20];
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
SOCKET ConnectSocket;
sockaddr_in clientService;
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
printf("\n Enter the Ip Address : ");
scanf("%s",ip);
clientService.sin_family = AF_INET;
clientService.sin_addr.S_un.S_addr = inet_addr(ip);
for(i=0;i<2;i++)
{
clientService.sin_port = htons((unsigned short)port[i]);
iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService));
if((iResult==0)||((iResult=WSAGetLastError())==WSAECONNREFUSED))
{
printf(" %d ",iResult);
printf("\n Port Number : %d",port[i]);
printf("\n Machine Found ");
flag=1;
break;
}
}
if(flag==0)
{
printf("\n Machine not found ");
}
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"closesocket function failed with machine %d error: %ld\n",i, WSAGetLastError());
WSACleanup();
return 1;
}
WSACleanup();
return 0;
}

After doing a non-blocking connect(), the socket will become writable when the connect completes. At that point you should check SO_LASTERROR on the socket to see if the connection has completed successfully.

Related

Trying to get started with winsock in C, how to keep connected?

I'm a beginner and trying to set up a TCP-client for the first time, using winsock. I put together a minimal client using some bits of code I found in examples (see below). It's basically working, i.e. I can receive the server's messages, and sending messages gives the expected results.
However, unless I add a loop which constantly does a receive/send routine (or even just the receive part) the connection is closed immediately after it has been established. Can I do something to keep the connection open and only receive or send something when there is demand for it?
The server is a closed source piece of software, so I have no idea about how it is set up.
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
int startWinsock(void) {
WSADATA wsa;
return WSAStartup(MAKEWORD(2, 2), &wsa);
}
int main(void) {
long rc;
SOCKET s;
SOCKADDR_IN addr;
printf("Starting Winsock... ");
rc = startWinsock();
if (rc != 0) {
printf("Error: unable to start Winsock, error code: %d\n", rc);
}
else {
printf("done.\n");
}
printf("Creating socket... ");
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) {
printf("Error: Unable to create socket, error code: %d\n", WSAGetLastError());
}
else {
printf("done.\n");
}
memset(&addr, 0, sizeof(SOCKADDR_IN));
addr.sin_family = AF_INET;
addr.sin_port = htons(10134);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
rc = connect(s, (SOCKADDR*)&addr, sizeof(SOCKADDR));
if (rc == SOCKET_ERROR) {
printf("Connection failed, error code: %d\n", WSAGetLastError());
}
else {
printf("Connected to 127.0.0.1.\n");
}
//if I add a loop here, which receives and/or sends stuff constantly, the connection stays established
return 0;
}
Very simple:
Your client tries to connect to the server.
If it succeeds, send a command, and read the response.
Keep receiving and sending until you're "done", then close the socket.
In other words:
You have control over when to close the connection - the socket will stay open until you close it.
Essentially, you're inventing your own custom network protocol
STRONG SUGGESTION:
Review Beej's Guide to Network Programming

UDP Server with 2 clients, how to assign a specific port to them

I have a small problem , I have a Server and Client applications.
And here what is I want, when I run Server + Client the messages are received from Port 15011 because I've bind it inside the Client file, but I want to run, 2 clients at once, then I message is received from Port 15011 and one is randomly assigned and I want the 2nd clients message to be received from 15012.
Should I have a IF statement that it check if that port free it is then take it else just that taken port + 1, is that even possible. Any suggestion would be great help to me.
Thanks in ahead !
// UDP client that uses blocking sockets
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include "conio.h"
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define SERVER_IP_ADDRESS "127.0.0.1" // IPv4 address of server
#define SERVER_PORT 15000 // Port number of server that will be used for communication with clients
#define BUFFER_SIZE 512 // Size of buffer that will be used for sending and receiving messages to client
int main()
{
// Server address structure
sockaddr_in serverAddress, clientAdress;
// Size of server address structure
int sockAddrLen = sizeof(serverAddress);
// Buffer that will be used for sending and receiving messages to client
char dataBuffer[BUFFER_SIZE];
// WSADATA data structure that is used to receive details of the Windows Sockets implementation
WSADATA wsaData;
// Initialize windows sockets for this process
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
// Check if library is succesfully initialized
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
// Initialize memory for address structure
memset((char*)&serverAddress, 0, sizeof(serverAddress));
// Initialize address structure of server
serverAddress.sin_family = AF_INET; // IPv4 address famly
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // Set server IP address using string
serverAddress.sin_port = htons(SERVER_PORT); // Set server port
memset((char*)&clientAdress, 0, sizeof(clientAdress));
// Initialize address structure of server
clientAdress.sin_family = AF_INET; // IPv4 address famly
clientAdress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // Set server IP address using string
clientAdress.sin_port = htons(15011); // Set server port
// Create a socket
SOCKET clientSocket = socket(AF_INET, // IPv4 address famly
SOCK_DGRAM, // Datagram socket
IPPROTO_UDP); // UDP protocol
iResult = bind(clientSocket, (SOCKADDR *)&clientAdress, sizeof(clientAdress));
// Check if socket creation succeeded
if (clientSocket == INVALID_SOCKET)
{
printf("Creating socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
while (1) {
printf("Enter message to send:\n");
// Read string from user into outgoing buffer
gets_s(dataBuffer, BUFFER_SIZE);
// Send message to server
iResult = sendto(clientSocket, // Own socket
dataBuffer, // Text of message
strlen(dataBuffer), // Message size
0, // No flags
(SOCKADDR *)&serverAddress, // Address structure of server (type, IP address and port)
sizeof(serverAddress)); // Size of sockadr_in structure
// Check if message is succesfully sent. If not, close client application
if (iResult == SOCKET_ERROR)
{
printf("sendto failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// Only for demonstration purpose
printf("Press any key to exit: ");
_getch();
// Close client application
iResult = closesocket(clientSocket);
if (iResult == SOCKET_ERROR)
{
printf("closesocket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Close Winsock library
WSACleanup();
// Client has succesfully sent a message
return 0;
}
I've added a simple If statement , if you can not connect to this port just connect to the next one "15012"
// UDP client that uses blocking sockets
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include "conio.h"
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define SERVER_IP_ADDRESS "127.0.0.1" // IPv4 address of server
#define SERVER_PORT 15000 // Port number of server that will be used for communication with clients
#define BUFFER_SIZE 512 // Size of buffer that will be used for sending and receiving messages to client
int main()
{
// Server address structure
sockaddr_in serverAddress, clientAdress;
// Size of server address structure
int sockAddrLen = sizeof(serverAddress);
// Buffer that will be used for sending and receiving messages to client
char dataBuffer[BUFFER_SIZE];
// WSADATA data structure that is used to receive details of the Windows Sockets implementation
WSADATA wsaData;
// Initialize windows sockets for this process
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
// Check if library is succesfully initialized
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
// Initialize memory for address structure
memset((char*)&serverAddress, 0, sizeof(serverAddress));
// Initialize address structure of server
serverAddress.sin_family = AF_INET; // IPv4 address famly
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // Set server IP address using string
serverAddress.sin_port = htons(SERVER_PORT); // Set server port
memset((char*)&clientAdress, 0, sizeof(clientAdress));
// Initialize address structure of server
clientAdress.sin_family = AF_INET; // IPv4 address famly
clientAdress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // Set server IP address using string
clientAdress.sin_port = htons(15011); // Set server port
// Create a socket
SOCKET clientSocket = socket(AF_INET, // IPv4 address famly
SOCK_DGRAM, // Datagram socket
IPPROTO_UDP); // UDP protocol
iResult = bind(clientSocket, (SOCKADDR *)&clientAdress, sizeof(clientAdress));
if (iResult == SOCKET_ERROR)
{
clientAdress.sin_family = AF_INET; // IPv4 address famly
clientAdress.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); // Set server IP address using string
clientAdress.sin_port = htons(15012); // Set server port
iResult = bind(clientSocket, (SOCKADDR *)&clientAdress, sizeof(clientAdress));
if (iResult == SOCKET_ERROR) {
printf("Socket bind failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// Check if socket creation succeeded
if (clientSocket == INVALID_SOCKET)
{
printf("Creating socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
while (1) {
printf("Enter message to send:\n");
// Read string from user into outgoing buffer
gets_s(dataBuffer, BUFFER_SIZE);
// Send message to server
iResult = sendto(clientSocket, // Own socket
dataBuffer, // Text of message
strlen(dataBuffer), // Message size
0, // No flags
(SOCKADDR *)&serverAddress, // Address structure of server (type, IP address and port)
sizeof(serverAddress)); // Size of sockadr_in structure
// Check if message is succesfully sent. If not, close client application
if (iResult == SOCKET_ERROR)
{
printf("sendto failed with error: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// Only for demonstration purpose
printf("Press any key to exit: ");
_getch();
// Close client application
iResult = closesocket(clientSocket);
if (iResult == SOCKET_ERROR)
{
printf("closesocket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Close Winsock library
WSACleanup();
// Client has succesfully sent a message
return 0;
}

Winsock Error 1013- Permission Denied

I'm trying to run the simple program below using C:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment (lib, "Ws2_32.lib")
int main(int argc, char **argv)
{
int iResult;
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
printf("Initialising Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
getchar();
return 1;
}
printf("Initialised.\n");
//Create a socket
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
getchar();
}
printf("Socket created.\n");
server.sin_addr.s_addr = inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons(80);
//Connect to remote server
iResult = connect(s, (struct sockaddr *)&server, sizeof (server));
if (iResult == SOCKET_ERROR)
{
printf("Connect function failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(s);
if (iResult == SOCKET_ERROR)
printf("closesocket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
getchar();
return 1;
}
printf("Connected");
getchar();
return 0;
}
This is basically a C program that create a socket and makes a connection to Google. (I'm just following the tutorial: http://www.binarytides.com/winsock-socket-programming-tutorial as I'm completly new to socket programming).
Now my program outputs:
Initialising Winsock...
Initialised.
Socket Created.
Connect function failed with error: 10013
After some research I found that this means that this is a permissions denied error.
I tried looking for some fixes such as running Visual Studio Express as Administrator and running these commands on my command prompt:
netsh winsock reset catalog
netsh int ip reset reset.log hit
and restart my computer but it still does not work.
Its worth mentioning when I ran the 2nd command: netsh int ip reset reset.log hit I got the following error message:
Resetting , failed.
Access is denied.
There's no user specified settings to be reset.
Even though I was running the command prompt as admin.
I also temporarily deactivated my Kaspersky Internet Security but still no fix. I am completely new to C and socket programming.
I've tried your code and I can use it to connect to some other machine. I made a little modification since I do not have a machine running a HTTP server at hand right now. Therefore I used www.google.com:
struct sockaddr_in server;
remoteHost = gethostbyname("www.google.com"); // get IP of www.google.com
server.sin_addr.s_addr = *((unsigned long *)remoteHost->h_addr); // inet_addr("74.125.235.20");
server.sin_family = AF_INET;
server.sin_port = htons(80);
The output of your application is:
Initialising Winsock...
Initialised.
Socket created.
Connected
So basically your implementation is correct.
This means that there is some other gremlin around! The MSDN description of error 10013 is:
WSAEACCES
10013 (0x271D)
An attempt was made to access a socket in a way forbidden by its access permissions.
This can be caused by antivirus or firewall software. Therefore try to disable your firewall and anti virus and run your application again.
If that works try to find the firewall/anti virus setting(s) which may block your application from connecting. This can be quite an ordeal (I know what I'm talking about)...

WinSock program in C, works only on local computer

I'm a newbie in Network Programming, started to learn how to use WinSock in C.
I don't have any knowledge in Networking right now.
Anyway, I've written the following code for Client and Server, using WinSock.
Server:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define MY_ERROR 1
#define PORT 7777
#define MAX_NUM_CLIENTS 1 /* I don't know how to thread right now. */
#define MAX_CLIENT_MSG_LEN 1000
int main()
{
WSADATA wsa;
SOCKET mySocket, acceptSocket;
struct sockaddr_in server, client;
int sockAddrInLength = sizeof(struct sockaddr_in);
char clientMessage[MAX_CLIENT_MSG_LEN];
int clientMessageLength;
char* message;
int running = 1;
if(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
return MY_ERROR;
}
printf("WSAStartup succeded.\n");
mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (mySocket == INVALID_SOCKET)
{
fprintf(stderr, "Socket creation failed.\n");
return MY_ERROR;
}
printf("Socket creation succeeded.\n");
server.sin_addr.s_addr = INADDR_ANY;
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
if (bind(mySocket, (struct sockaddr*) &server, sizeof server) == SOCKET_ERROR)
{
fprintf(stderr, "Binding socket on port %d failed.\n", PORT);
return MY_ERROR;
}
printf("Binding socket on port %d successfully.\n", PORT);
while (running)
{
listen(mySocket, MAX_NUM_CLIENTS);
printf("Waiting for a connection...\n");
acceptSocket = accept(mySocket, (struct sockaddr*) &client, &sockAddrInLength);
if (acceptSocket == INVALID_SOCKET)
{
fprintf(stderr, "Accept failed.\n");
return MY_ERROR;
}
printf("Accept succeeded.\n");
if ((clientMessageLength = recv(acceptSocket, clientMessage, sizeof clientMessage, 0)) == SOCKET_ERROR)
{
fprintf(stderr, "Recv failed.\n");
return MY_ERROR;
}
printf("Recv succeeded.\n");
printf("Data:\n");
clientMessage[clientMessageLength] = NULL; /* Null terminator */
printf("Client: %s\n", clientMessage);
message = "Hello client, I'm the server. Bye bye. :-)\n";
if (send(acceptSocket, message, strlen(message), 0) < 0)
{
fprintf(stderr, "Send failed.\n");
return MY_ERROR;
}
printf("Send succeded.\n");
}
closesocket(mySocket);
WSACleanup();
getchar();
return 0;
}
And this is the code for the Client:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#define IP /* My IP */
#define MY_ERROR 1
#define PORT 7777
#define MAX_SERVER_MSG_LEN 1000
int main()
{
WSADATA wsa;
SOCKET mySocket;
struct sockaddr_in server;
char* message;
char serverMessage[MAX_SERVER_MSG_LEN];
int serverMessageLength;
if(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
getchar();
return MY_ERROR;
}
printf("WSASucceeded.\n");
mySocket = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
if (mySocket == INVALID_SOCKET)
{
fprintf(stderr, "Socket creation failed.\n");
getchar();
return MY_ERROR;
}
server.sin_addr.s_addr = inet_addr(IP);
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
if (connect(mySocket, (struct sockaddr*) &server, sizeof server) < 0)
{
fprintf(stderr, "Connection failed. error: %s\n",WSAGetLastError());
getchar();
return MY_ERROR;
}
printf("Connection established.\n");
message = "Hello server, I'm the sweet client. :-)\n";
if (send(mySocket, message, strlen(message), 0) < 0)
{
fprintf(stderr, "Sending failed.\n");
return MY_ERROR;
}
printf("Sending succeeded.\n");
if ((serverMessageLength = recv(mySocket, serverMessage, sizeof serverMessage, 0)) == SOCKET_ERROR)
{
fprintf(stderr, "Recv failed.\n");
return MY_ERROR;
}
printf("Recv succeeded.\n");
printf("Data:\n");
serverMessage[serverMessageLength] = NULL;
printf("Server: %s", serverMessage);
closesocket(mySocket);
getchar();
return 0;
}
Ok.
When I run both server and client on my comupter, and define IP (in the client code) to be my internal IP (I think, becuase it is the form of 192.168.x.x) it works fine.
When I define IP to be my external IP, it doesn't work on my computer (when I run both program in my computer). The connection failes.
In addition -
When I'm trying to run the client on any other computer, it doesn't work (when IP is defined to be my internal or my external IP), the connection failes.
My questions are:
Why when I run both server & client on my computer, it works just with my internal IP?
What should I do in order that the client could be run on another computer and it will work?
I know that maybe the answer contains terms like "internal/external IPs", "routers", maybe "firewall" or "port forwarding".
Just remember I'm really a newbie in Networking and I don't have any knowledge about those terms, so please: I hope your explnations will be beginner-friendly.
I want to start learning Networking, but my first step is to understand how to use Sockets, and I have a problem in the connetction with other computers.
If you have any articles/something like that in order to give me a better understanding of the problem, it could help too.
Thank you very much! :)
This doesn't look like a coding problem, but a network configuration problem.
Let me try and rephrase what you're saying : Please correct me if I've misunderstood:-
You have an "internal" network (192.168.x.y).
You run the client on one PC on your internal network, and the server on another.
It all works so far.
But, you're also connected to the internet, via a different 'external' IP address
- When your client tries to access the server via this address, it fails. (The exact error code from WSAGetLastError is useful here, please)
At this point, it's down to how you're connected to the net. Let's assume a typical home office scenario. You have broadband, with an ADSL router/modem giving you network connectivity. Now, while the router will have an 'external' IP address, PC's on your local network typically do NOT. So, if you try connecting to the 'external' IP, you're actually trying to talk to the router.
This is where Port forwarding comes into play. You need to configure the router to forward port 7777 to the correct IP address for your server on the internal network. You'll find a screen like this on your router somewhere.
You would enter port 7777 for both 'from' and 'to', enable TCP, and specify the LAN address of your server. The router will then forward incoming connections on port 7777 to the specified server.

WSA Connection refused even though the host is not present

I'm writing a code to discover the devices on the network and this is just a part of it.
I'm trying to discover a host by establishing an socket connection with the host on port 80.
It connects well on some of the hosts on port 80 as the devices are listening on port 80. At times the connect function returns error as WSACONNECTIONREFUSED as there is no web service running on the host.
It is surprising to see that when i try to establish an socket connection on invalid ip addresses I get WSACONNECTIONREFUSED instead of WSAETIMEDOUT.
I googled and found out that the antivirus and firewall can cause the problems and i have disabled both on the scanning machine but no luck what could be causing the problem?
I have posted the code below:
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
{
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET)
{
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("192.168.4.28"); //No Host by this ip address
clientService.sin_port = htons(80); //Port is 80
iResult = connect(ConnectSocket, (SOCKADDR *) &clientService,
sizeof(clientService));
printf("The socket connect return status : %d ", iResult);
if (iResult == SOCKET_ERROR)
{
wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR)
wprintf(L"closesocket function failed with error: %ld\n",
WSAGetLastError());
WSACleanup();
return 1;
}
wprintf(L"Connected to server.\n");
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR)
{
printf("\n socket Connection failed ");
WSACleanup();
return 1;
}
WSACleanup();
return 0;
}

Resources