Winsock erstellen
Die Werbung stört? Registriere dich
hier!
Hey Leute,
ich hab mich drangesetzt und versucht ein Winsock zu proggen (Visual basic 6).
Es soll Packete lesen (zb. von einem privaten Server; in diesem Fall ein Flyff Pserver) und zurücksenden. Könnt ihr euch mein Code bitte angucken? :D
Seid nicht so streng, bin noch nicht so gut darin! *_*
//Main.cpp
#include <iostream>
#include "Socket.h"
using namespace std;
int main()
{
int choice;
int port = 666;
//char *ipAddress = "127.0.0.1";
string ipAddress;
bool done = false;
char recMessage[STRLEN];
char sendMessage[STRLEN];
cout<<"1) Host server"<<endl;
cout<<"2) Join server"<<endl;
cout<<"3) Quit"<<endl;
cin>>choice;
if ( choice == 3 )
exit(0);
else if ( choice == 2 )
{
//Client
cout<<"Enter an IP address, 127.0.0.1 is the loopback address"<<endl;
cin>>ipAddress;
ClientSocket sockClient;
cout<<"ATTEMPTING TO CONNECT..."<<endl;
sockClient.ConnectToServer( ipAddress.c_str(), port );
//Connected
while ( !done )
{
sockClient.GetAndSendMessage();
cout<<"\t--WAIT--"<<endl;
sockClient.RecvData( recMessage, STRLEN );
cout<<"Recv > "<<recMessage<<endl;
if ( strcmp( recMessage, "end" ) == 0 ||
strcmp( sendMessage, "end" ) == 0 )
{
done = true;
}
}
sockClient.CloseConnection();
}
else if ( choice == 1 )
{
//SERVER
ServerSocket sockServer;
cout<<"HOSTING..."<<endl;
sockServer.StartHosting( port );
//Connected
while ( !done )
{
cout<<"\t--WAIT--"<<endl;
sockServer.RecvData( recMessage, STRLEN );
cout<<"Recv > "<<recMessage<<endl;
sockServer.GetAndSendMessage();
if ( strcmp( recMessage, "end" ) == 0 ||
strcmp( sendMessage, "end" ) == 0 )
{
done = true;
}
}
}
}
//Socket.cpp
#include "Socket.h"
Socket::Socket()
{
if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
{
cerr<<"Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}
//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if ( mySocket == INVALID_SOCKET )
{
cerr<<"Socket Initialization: Error creating socket"<<endl;
system("pause");
WSACleanup();
exit(11);
}
myBackup = mySocket;
}
Socket::~Socket()
{
WSACleanup();
}
bool Socket::SendData( char *buffer )
{
send( mySocket, buffer, strlen( buffer ), 0 );
return true;
}
bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );
buffer[i] = '\0';
return true;
}
void Socket::CloseConnection()
{
//cout<<"CLOSE CONNECTION"<<endl;
closesocket( mySocket );
mySocket = myBackup;
}
void Socket::GetAndSendMessage()
{
char message[STRLEN];
cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
cout<<"Send > ";
cin.get( message, STRLEN );
SendData( message );
}
void ServerSocket::StartHosting( int port )
{
Bind( port );
Listen();
}
void ServerSocket::Listen()
{
//cout<<"LISTEN FOR CLIENT..."<<endl;
if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Error listening on socket\n";
system("pause");
WSACleanup();
exit(15);
}
//cout<<"ACCEPT CONNECTION..."<<endl;
acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
}
void ServerSocket::Bind( int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
myAddress.sin_port = htons( port );
//cout<<"BIND TO PORT "<<port<<endl;
if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
{
cerr<<"ServerSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(14);
}
}
void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = inet_addr( ipAddress );
myAddress.sin_port = htons( port );
//cout<<"CONNECTED"<<endl;
if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
{
cerr<<"ClientSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(13);
}
}
Das meiste hat nen Kumbel von mir gemacht. Ich hab nur ca. 30% gemacht ^^
Vielen Dank für die Hilfe. Nehme auch gern Kritik an :)
mfG Fischiiiii
Lesezeichen