Sockets windows c++

Hello guys, i'v spent a lot of time on the msdn page about sockets
Now i've got this code that is compiling without any error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Socket.cpp : definisce il punto di ingresso dell'applicazione console.
//
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include "stdafx.h"
#include <iostream>
#include <WinSock2.h>
#include <stdio.h>
#include <WS2tcpip.h>
#include <Windows.h>
#define DEFAULT_PORT "27015"
#define DEFAULT_BUFLEN 512
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
using namespace std;

struct addrinfo *result = NULL,*ptr = NULL,	hints;


int main(int argc, char** argv)
{


	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	WSADATA wsaData;
	int Result;
	Result = WSAStartup(MAKEWORD(2,2),&wsaData);
	if (Result != 0) {
		cout << "Startup failed! -"<<Result<<endl;
		Sleep(1000);
		return 1;
	}
	Result = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
	if (Result != 0) {
		cout<<"Error getting addrinfo - "<<Result<<endl;
		WSACleanup();
		Sleep(1000);
		return 1;
	}

	SOCKET ConnectSocket = INVALID_SOCKET;

	ptr = result;
	ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,ptr->ai_protocol);
	
	if (ConnectSocket == INVALID_SOCKET) {
		cout << "Error creating socket! - " << WSAGetLastError() << endl;
		freeaddrinfo(result);
		WSACleanup;
		Sleep(1000);
		return 1;

	}
	
	Result = connect(ConnectSocket,ptr->ai_addr, (int)ptr->ai_addrlen);
	if (Result == SOCKET_ERROR) {
		closesocket(ConnectSocket);
		cout << "Invalid socket!\n";

	}
	freeaddrinfo(result);
	if (ConnectSocket == INVALID_SOCKET) {
		cout << "Can't connect to server!\n";
		WSACleanup();
		return 1;
	}

	int recvbuflen = DEFAULT_BUFLEN;
	char *sendbuf = "test";
	char recvbuf[DEFAULT_BUFLEN];

	int iResult;
	iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
	if (iResult == SOCKET_ERROR) {
		cout << "Send failed! -" << WSAGetLastError();
		closesocket(ConnectSocket);
		WSACleanup;
		Sleep(10000);
		return 1;
	}
	cout << "Bytes Sent : " << iResult<<endl;


	do {
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0) {
			cout << "Bytes recived: " << iResult<<endl;
		}
		else if (iResult==0){
			cout << "Connection Closed.\n";
		}
		else {
			cout << "Recive failed\n" << WSAGetLastError();
		}
	} while (iResult > 0);

	iResult = shutdown(ConnectSocket, SD_SEND);
	if (iResult == SOCKET_ERROR) {
		cout << "Shutdown failed! -" << WSAGetLastError();
		closesocket(ConnectSocket);
		WSACleanup();
		Sleep(1000);
		return 1;
	}


	Sleep(1000);
	return 0;
}

the output is
Invalid socket!
Send failed! - 10038
i didn't understand: how and where i can specify the server's ip?
Thanks for the answers!
Last edited on
Hi,
maybe you should try WSAGetLastError after trying to initialize your socket, get the return value of the function "connect" to the output as well.

That error code you get seems to be the one that is returned when trying "send" on a socket who's initialization failed. Then you can return if the Socket is not initialzed properly

Nothing more i can say about that, sorry,
...
"Invalid socket!" appears when the connection on line 62 failed. This happens when no one is listening to that address/port.

Line 69 shouldn't be there. It is already done on line 53.

Always use WSAGetLastError to determine what exactly went wrong.
i've got a simple client / server progs that work if you're interested. simple they are but work. as examples
Last edited on
Topic archived. No new replies allowed.