Heres the code, for a basic client/server app which i got from a tutorial as a base.
Code:
#include <iostream.h>
#include <winsock2.h>
const unsigned int maxchars = 200;
void client()
{
char dest_ip[15];
cout << "Destination IP Address: ";
cin >> dest_ip;
cout << endl;
SOCKET exp;
if (exp = socket(AF_INET,SOCK_STREAM,0))
cout << "--- Sending socket created successfully." << endl;
else
{
cout << "Can't create sending socket. Aborting." << endl;
WSACleanup();
exit(0);
}
sockaddr_in target;
target.sin_family = AF_INET;
target.sin_port = htons(5656);
target.sin_addr.s_addr = inet_addr(dest_ip);
if (connect(exp,(LPSOCKADDR)&target,sizeof(target)) == SOCKET_ERROR)
{
cout << "Can't connect. Aborting." << endl;
WSACleanup();
exit(0);
}
else
{
cout << "--- Connected successfully." << endl;
cout << " -| Enter 666 when you want to end the session." << endl;
char buff[maxchars];
while(buff[0] != '6' || buff[1] != '6' || buff[2] != '6')
{
cin.getline(buff,maxchars);
cout << "Enter data: ";
if (buff[0] != '6' || buff[1] != '6' || buff[2] != '6')
send(exp,buff,sizeof(buff),0);
else
{
send(exp,buff,sizeof(buff),0);
closesocket(exp);
cout << "--- Session closed." << endl;
WSACleanup();
exit(0);
}
}
}
}
void server()
{
SOCKET imp;
if (imp = socket(AF_INET,SOCK_STREAM,0))
cout << "--- Receiving socket created successfully." << endl;
else
{
cout << "Can't create receiving socket. Aborting." << endl;
WSACleanup();
exit(0);
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5656);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(imp,(LPSOCKADDR)&addr,sizeof(addr)) == SOCKET_ERROR)
{
cout << "Error binding. Aborting." << endl;
WSACleanup();
exit(0);
}
if (listen(imp,1) == SOCKET_ERROR)
{
cout << "Error listening. Aborting." << endl;
closesocket(imp);
WSACleanup();
exit(0);
}
else
{
SOCKET client;
client == accept(imp,NULL,NULL);
if (client == INVALID_SOCKET)
{
cout << "Can't accept connection. Aborting." << endl;
closesocket(imp);
WSACleanup();
exit(0);
}
else
{
cout << endl;
char buffer[maxchars];
while(buffer[0] != '6' || buffer[1] != '6' || buffer[2] != '6')
{
buffer[0] = 6;
buffer[1] = 6;
buffer[2] = 6;
recv(client,buffer,sizeof(buffer),0);
if (buffer[0] != '6' || buffer[1] != '6' || buffer[2] != '6')
{
if(strlen(buffer) > 0)
cout << "Message Received: " << buffer << endl;
}
else
{
closesocket(imp);
cout << "--- Session closed." << endl;
WSACleanup();
exit(0);
}
}
}
}
}
void main()
{
WSADATA w;
if(WSAStartup(0x0202,&w))
{
cout << "Can't initialize WinSock. Aborting." << endl;
exit(0);
}
else
cout << "--- WinSock initialized successfully." << endl;
if (w.wVersion != 0x0202)
{
cout << "Wrong version. Aborting." << endl;
WSACleanup();
exit(0);
}
else
cout << "--- Version 2.2 accepted." << endl << endl;
int o = 1;
while (o != 1 && o != 2 && o != 3);
{
cout << "1 - Run Server" << endl;
cout << "2 - Run Client" << endl;
cout << "3 - Exit" << endl;
cout << "Enter 1,2 or 3: ";
cin >> o;
}
switch(o)
{
case 1:
server();
break;
case 2:
client();
break;
case 3:
exit(0);
break;
};
}
When I connect to the server with the client the only message thats repeated constantly in the server is this:
Message Received: ♠♠♠⌂≈