Learning how to link multiple players to one host would be nice.
Ideally, I'd like to see two example Python scripts for "Hello World" sent via socket, assuming that the IP of both computers would be entered manually.
Unfortunately, I rarely work in Python. The best I could do is C#, or C++ because those are the ones I generally use. It's pretty similar, you'd probably just have to change the names.
I wrote the C++ about 6 months ago, so its not very well written.
I stripped a lot out of the C# example, so it should be pretty simple. It could be simpler still, but its multi-threaded to allow the program to run other things while sending and receiving.
#include "main.hpp"
using namespace std;
void zero_char_array(char *zeroed, unsigned size)
{
for (unsigned i = 0; i < size; i++)
zeroed[i] = 0;
}
void server()
{
WSAData windows_socket_data;
WORD dll_version = MAKEWORD(2,1);
string message;
char received[1024];
int success = WSAStartup(dll_version, &windows_socket_data);
SOCKADDR_IN address;
int address_size = sizeof(address);
SOCKET socket_listen;
SOCKET socket_connect;
socket_connect = socket(AF_INET, SOCK_STREAM, NULL);
address.sin_addr.s_addr = inet_addr("127.0.0.2");
address.sin_family = AF_INET;
address.sin_port = htons(444);
socket_listen = socket(AF_INET, SOCK_STREAM, NULL);
bind(socket_listen, (SOCKADDR*)&address, address_size);
listen(socket_listen, SOMAXCONN);
while(1)
{
cout << "\n\tSERVER: Waiting..." << endl;
socket_connect = accept(socket_listen, (SOCKADDR*)&address, &address_size);
if (socket_connect)
{
cout << "Connected!" << endl;
while (true)
{
zero_char_array(received, sizeof(received));
success = recv(socket_connect, received, sizeof(received), NULL);
message = received;
success = send(socket_connect, received, sizeof(received), NULL);
if (message == "quit")
break;
}
cout << "Connection Ended" << endl;
}
}
closesocket(socket_connect);
closesocket(socket_listen);
}
void client()
{
WSAData windows_socket_data;
WORD dll_version = MAKEWORD(2,1);
long success = WSAStartup(dll_version, &windows_socket_data);
string response;
string convert;
string connect_address;
char received_message[1024];
SOCKADDR_IN address;
SOCKET connect_socket;
connect_socket = socket(AF_INET, SOCK_STREAM, NULL);
cout << "\tConnect Address: ";
cin >> connect_address;
address.sin_addr.s_addr = inet_addr(connect_address.c_str());
address.sin_family = AF_INET;
address.sin_port = htons(444);
connect(connect_socket, (SOCKADDR*)&address, sizeof(address));
while (true)
{
zero_char_array(received_message, sizeof(received_message));
cin >> response;
success = send(connect_socket, response.c_str(), response.length(), NULL);
success = recv(connect_socket, received_message, sizeof(received_message), NULL);
response = received_message;
if (response != "quit")
cout << "\n\t" << response << endl;
else
break;
}
}
int main()
{
char choice;
cout << "C - Start Client" << endl;
cout << "S - Start Server" << endl;
cout << "Q - Quit" << endl;
cin >> choice;
switch (choice)
{
case 'C':
case 'c':
client();
break;
case 'S':
case 's':
server();
break;
default:
break;
};
cin.get();
cin.get();
return 0;
}
public class Transfer
{
public Transfer(string Address)
{
Self = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
SelfAddress = IPAddress.Parse(Address);
Information = new InformationHandler.Store();
ReadTemporary = new InformationHandler.Store();
}
public bool Send(System.Net.Sockets.Socket Handler, byte[] ToBeSent)
{
try
{
if (Handler.Connected)
{
SendComplete.Reset();
Handler.BeginSend(ToBeSent, 0, ToBeSent.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(SendCallback), Handler);
SendComplete.WaitOne();
}
}
catch (Exception E)
{
System.Windows.Forms.MessageBox.Show(E.ToString());
return false;
}
return true;
}
public void SendCallback(IAsyncResult Result)
{
System.Net.Sockets.Socket Handler = (System.Net.Sockets.Socket)Result.AsyncState;
int SentBytes = Handler.EndSend(Result);
SendComplete.Set();
}
public bool Receive(System.Net.Sockets.Socket Handler)
{
try
{
if (Handler.Connected)
{
ReceiveComplete.Reset();
Client State = new Client();
State.WorkSocket = Handler;
Handler.BeginReceive(State.Buffer, 0, State.Buffer.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(ReceiveCallBack), State);
ReceiveComplete.WaitOne();
}
}
catch (Exception E)
{
System.Windows.Forms.MessageBox.Show(E.ToString());
}
return true;
}
public void ReceiveCallBack(IAsyncResult Result)
{
Client State = (Client)Result.AsyncState;
System.Net.Sockets.Socket Handler = State.WorkSocket;
try
{
int ReadBytes = Handler.EndReceive(Result);
if (ReadBytes > 0)
{
string ReceiveString = Encoding.ASCII.GetString(State.Buffer, 0, ReadBytes);
InformationHandler.Store TemporaryReceived = new InformationHandler.Store();
TemporaryReceived.AddInformation(ReceiveString);
Information.SeperateByWord(TemporaryReceived);
ReceiveComplete.Set();
}
}
catch (Exception E)
{
System.Windows.Forms.MessageBox.Show(E.ToString());
}
}
public System.Net.Sockets.Socket Self;
public IPAddress SelfAddress;
public IPEndPoint SelfEndPoint { get; set; }
public InformationHandler.Store ReadTemporary { get; set; }
public InformationHandler.Store Information { get; set; }
public System.Threading.ManualResetEvent ReceiveComplete = new System.Threading.ManualResetEvent(false);
public System.Threading.ManualResetEvent SendComplete = new System.Threading.ManualResetEvent(false);
public byte[] BytesReceieved;
private int ReceivedLength;
}
private void Connect()
{
SelfClient.Self.BeginConnect("127.0.0.1", 8000, new AsyncCallback(SelfClient.ClientConnect), SelfClient.Self);
}
public void ClientConnect(IAsyncResult Result)
{
try
{
Self = (System.Net.Sockets.Socket)Result.AsyncState;
Self.EndConnect(Result);
Connecting.Set();
}
catch (Exception E)
{
System.Windows.Forms.MessageBox.Show(E.ToString());
}
}
public void Accept_Listen(IAsyncResult Result)
{
Accepted.Set();
System.Net.Sockets.Socket Listener = (System.Net.Sockets.Socket)Result.AsyncState;
System.Net.Sockets.Socket Handler = Listener.EndAccept(Result);
Client State = new Client();
State.WorkSocket = Handler;
State.ClientIndex = -1;
Handler.BeginReceive(State.Buffer, 0, State.Buffer.Length, 0, new AsyncCallback(HandleClientInput), State);
}
public void WaitForConnection(object Sender, System.ComponentModel.DoWorkEventArgs E)
{
System.Net.Sockets.Socket Listener = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
SelfEndPoint = new IPEndPoint(IPAddress.Any, 8000);
Self.Bind(SelfEndPoint);
try
{
Self.Listen(100);
while (true)
{
Accepted.Reset();
Self.BeginAccept(new AsyncCallback(Accept_Listen), Self);
Accepted.WaitOne();
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString());
System.Windows.Forms.Application.ExitThread();
}
}
If you need any help, just ask.