95590
Feb 24, 2011
4081
2
0
Socket (winsock)
Hi Leute!
Hab des Problem, dass ich keine Verbindung bekomme. Hab schon gegoogelt und komm nicht weiter. Überall wird es so beschrieben.
Hoff ihr könnt mir helfen!
Vielen Dank!
Hab des Problem, dass ich keine Verbindung bekomme. Hab schon gegoogelt und komm nicht weiter. Überall wird es so beschrieben.
Hoff ihr könnt mir helfen!
Vielen Dank!
#include "stdafx.h"
#pragma comment(lib, "wsock32.lib")
#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <string>
#include <iostream>
using namespace std;
SOCKET Socket;
SOCKADDR_IN addr;
int main()
{
WSADATA wsa;
int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
if(rc == SOCKET_ERROR){
cout << "Error while starting server... \n";
}
Socket = socket(AF_INET, SOCK_STREAM, 0);
if(Socket == INVALID_SOCKET){
std::cout << "Error while building socket. \n";
}
addr.sin_port = htons(1234);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(Socket, (SOCKADDR*)&addr, sizeof(SOCKADDR)) == 0)
{
cout << "Connection with server " << inet_ntoa(addr.sin_addr)
<< " established." << endl << endl;
closesocket(Socket);
system("pause");
}
else
{
cout << "Impossible to connect to server " << inet_ntoa(addr.sin_addr)
<< ":" << addr.sin_port
<< ". Please check host and port. \n";
closesocket(Socket);
system("pause");
return 1;
}
}
Please also mark the comments that contributed to the solution of the article
Content-Key: 161524
Url: https://administrator.de/contentid/161524
Printed on: April 19, 2024 at 17:04 o'clock
2 Comments
Latest comment