bueno mira primero ve a:
settings despues compiler luego en la pestaña linker settings despues en donde dice other linker opcions agrgas esto -lws2_32.
aqui estan los codigo que te deci vangod.
Código C++:
Ver original// Netzwerkprogrammierung, WINSOCK API, Sockets
// TCP Server
#pragma comment(lib, "ws2_32.lib")
#include <cstdio>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
using namespace std;
char recvbuf[256];
int main()
{
// Hilfsvariable
long res;
// Versionsdaten
WSADATA wsaData;
// ws2_32.dll aktivieren
res = WSAStartup(MAKEWORD(2, 1), &wsaData);
if(res == 0 )
cout << "WSAStartup()\t\t successful" << endl;
else
cout << "error WSAStartup(): " << WSAGetLastError() << endl;
// Socket Deskriptoren
SOCKET slisten, client;
// Socket anforder bzw. Kommunikationsendpunkt einrichten
slisten = socket(AF_INET, SOCK_STREAM, 0);
if(slisten != INVALID_SOCKET )
cout << "socket() \t\t successful" << endl;
else
cout << "error socket(): " << WSAGetLastError() << endl;
// Verbindungsinformationen
sockaddr_in info; // sockaddr
info.sin_addr.s_addr = inet_addr("127.0.0.1");
info.sin_family = AF_INET;
info.sin_port = htons(54345);
int infolen = sizeof(info);
res = bind(slisten, (struct sockaddr*)&info, infolen);
if(res != SOCKET_ERROR )
cout << "bind() \t\t successful" << endl;
else
cout << "error bind(): " << WSAGetLastError() << endl;
// Warteschlange für einkommende Verbindungen
res = listen(slisten, SOMAXCONN);
if(res != SOCKET_ERROR )
cout << "listen() \t\t successful" << endl;
else
cout << "error listen(): " << WSAGetLastError() << endl;
sockaddr_in clientinfo;
int clientinfolen = sizeof(clientinfo);
while(1)
{
// Verbindung aus der Warteschlange abholen
client = accept(slisten, (struct sockaddr*)&clientinfo, &clientinfolen);
if(client != SOCKET_ERROR)
cout << "client accepted: " << inet_ntoa(clientinfo.sin_addr) << ":"
<< ntohs(clientinfo.sin_port) << endl;
// über das Socket kommunizieren
res
= send
(client
, "server message received", strlen("server message received"), 0); if(res == SOCKET_ERROR)
cout << "error send()" << WSAGetLastError() << endl;
res = recv(client, recvbuf, 256, 0);
if(res == SOCKET_ERROR)
cout << "error recv(): " << WSAGetLastError() << endl;
else
cout << res << " bytes received: "
<< recvbuf << endl;
}
closesocket(client);
closesocket(slisten);
WSACleanup();
return 0;
}
Código C++:
Ver original// TCP Client
#pragma comment(lib, "ws2_32.lib")
#include <cstdio>
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
using namespace std;
char sendbuf[256];
char recvbuf[256];
int main()
{
long rc;
WSADATA wsaData;
SOCKET sConnect;
sockaddr_in conpar;
// ws2_32.dll aktivieren
rc = WSAStartup(MAKEWORD(2, 0), &wsaData);
if(rc == 0 )
cout << "WSAStartup()\t\t successful" << endl;
else
cout << "error WSAStartup(): " << WSAGetLastError() << endl;
// socket einrichten
sConnect = socket(AF_INET, SOCK_STREAM, 0);
if(sConnect != INVALID_SOCKET )
cout << "socket() \t\t successful" << endl;
else
cout << "error socket(): " << WSAGetLastError() << endl;
// verbindungsparameter
conpar.sin_addr.s_addr = inet_addr("127.0.0.1");
conpar.sin_family = AF_INET;
conpar.sin_port = htons(54345);
int conparlen = sizeof(conpar);
// server vor client starten, oder hier eine connect-schleife erstellen
rc = connect(sConnect, (struct sockaddr*)&conpar, conparlen);
if(rc != SOCKET_ERROR)
cout << "connect() \t\t successful" << endl;
else
cout << "not connected(): " << WSAGetLastError() << endl;
while(1)
{
memset(&sendbuf
, 0, sizeof(sendbuf
)); memset(&recvbuf
, 0, sizeof(recvbuf
));
rc = recv(sConnect, recvbuf, 256, 0);
if(rc == SOCKET_ERROR)
cout << "error recv(): " << WSAGetLastError() << endl;
else
{
cout << rc << " bytes received: "
<< recvbuf << endl;
}
cout << "send: ";
cin.getline(sendbuf, 256);
rc
= send
(sConnect
, sendbuf
, strlen(sendbuf
), 0); if(rc == SOCKET_ERROR)
cout << "error send(): " << WSAGetLastError() << endl;
else
cout << rc << " bytes sent" << endl;
}
closesocket(sConnect);
WSACleanup();
return 0;
}