 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Fri Jul 16, 2010 3:30 pm Post subject: |
|
|
| iPromise wrote: | Still fails to connect to myself..?
My whole current source - code for Winsock: |
Seriously... learn to program: look up the functions you are using on msdn. You will find out that they are 'blocking', meaning they will block the thread until something happens. Therefore you can not connect to yourself this way. Either use two threads or use a non-blocking form of winsock. Also, you need to call accept() after listen() to accept incoming connections.
|
|
| Back to top |
|
 |
iPromise Grandmaster Cheater
Reputation: -1
Joined: 27 Jun 2009 Posts: 529 Location: Canada
|
Posted: Sun Jul 18, 2010 10:13 pm Post subject: |
|
|
After about a day of MSDNing and googling, I came up with good working code I love it!
| Code: |
#include <WinSock2.h>
#include <iostream>
#include <string>
using namespace std;
void Host ( void )
{
WSADATA WSAData;
WSAStartup(MAKEWORD(2, 0), &WSAData);
if ( WSAData.wVersion >= 2 )
{
cout << "Success: Your Winsock version is compatible with the software." << endl;
}
else
{
cout << "Error: Your Winsock version isn't compatible with the software." << endl;
}
SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
int Connectivity;
sockaddr_in sockaddr;
sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(8888);
Connectivity = bind(Socket, (LPSOCKADDR) &sockaddr, sizeof(sockaddr));
if ( Connectivity == SOCKET_ERROR )
{
cout << "Error: Failed to bind the socket with the 'sockaddr' structure." << endl;
}
else
{
cout << "Success: Successfully binded the socket with the 'sockaddr' structure." << endl;
}
SOCKET Client = INVALID_SOCKET;
while ( Client == INVALID_SOCKET )
{
Connectivity = listen(Socket, 10);
if ( Connectivity == SOCKET_ERROR )
{
cout << "Error: Failed to listen to connections." << endl;
}
else
{
cout << "Success: Successfully listening to connections." << endl;
}
Client = accept(Socket, NULL, NULL);
if ( Client == INVALID_SOCKET )
{
cout << "Error: No connectivity from 'Client'.\n\n" << endl;
}
else
{
cout << "Success: Connectivity from 'Client'.\n\n" << endl;
}
Sleep(1000);
}
// Send and receive messages
bool Session = false;
while ( !Session )
{
string Message;
cout << endl << "Send Message: "; cin >> Message; cout << endl;
send( Client, Message.c_str(), Message.length(), 0 );
char Buffer[500] = {0};
recv( Client, Buffer, 500, 0 );
cout << "User sent: " << Buffer << endl;
if ( !strcmp(Message.c_str(), "quit") )
{
Session = true;
}
Sleep(100);
}
closesocket(Client);
closesocket(Socket);
WSACleanup();
}
void Join ( void )
{
WSADATA WSAData;
WSAStartup(MAKEWORD(2, 0), &WSAData);
if ( WSAData.wVersion >= 2 )
{
cout << "Success: Your Winsock version is compatible with the software." << endl;
}
else
{
cout << "Error: Your Winsock version isn't compatible with the software." << endl;
}
SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
sockaddr_in sockaddr;
sockaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(8888);
int Connectivity = INVALID_SOCKET;
while ( Connectivity == INVALID_SOCKET )
{
Connectivity = connect( Socket, (LPSOCKADDR) &sockaddr, sizeof(sockaddr) );
if ( Connectivity == INVALID_SOCKET )
{
cout << "Error: Failed to connect." << endl;
}
else
{
cout << "Success: Connected successfully." << endl;
}
Sleep(1000);
}
// Send and receive messages
bool Session = false;
while ( !Session )
{
string Message;
cout << endl << "Send Message: "; cin >> Message; cout << endl;
send( Socket, Message.c_str(), Message.length(), 0 );
char Buffer[500] = {0};
recv( Socket, Buffer, 500, 0 );
cout << "User sent: " << Buffer << endl;
if ( !strcmp(Message.c_str(), "quit") )
{
Session = true;
}
Sleep(100);
}
closesocket(Socket);
}
void main ( void )
{
system("TITLE C++ - Winsock");
system("COLOR 3");
int Choice = 0;
cout << "1) Host server\n";
cout << "2) Join server\n";
cout << endl << "Choose: "; cin >> Choice; cout << endl;
if ( Choice == 1 )
{
Host();
}
else if ( Choice == 2 )
{
Join();
}
}
|
This was ofcourse a console program and was just to test out my winsock with myself.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|