10.08.2017, 14:45
Hello people,
I'm currently getting into socket programming altogether with SAMP. I have client application(simple asynch tcp socket) and server(listen_server.pwn filterscript that u can find on this forum). I can send data from client to server, connect to server but i can't send data from server to client. My server goes insta offline.
making & connecting socket
socket_send()
c# receive function and also main
main
I'm currently getting into socket programming altogether with SAMP. I have client application(simple asynch tcp socket) and server(listen_server.pwn filterscript that u can find on this forum). I can send data from client to server, connect to server but i can't send data from server to client. My server goes insta offline.
making & connecting socket
Code:
new Socket:g_Socket; g_Socket = socket_create(TCP); if(is_socket_valid(g_Socket)) { socket_set_max_connections(g_Socket, 2500); socket_listen(g_Socket, 2526); // we are going to listen on port 1337 }
Code:
public onSocketRemoteConnect(Socket:id, remote_client[], remote_clientid) { printf("Dolazeca konekcija sa [%d:%s]", remote_clientid, remote_client); // [id:ip] if(is_socket_valid(id)) socket_send(id, "Welcome :)", strlen("Welcome :)")); return 1; }
Code:
private static void ReceiveCallback(IAsyncResult ar) { try { StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; int bytesRead = client.EndReceive(ar)+1; if (bytesRead > 0) { state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } else { if (state.sb.Length > 1) { response = state.sb.ToString(); } receiveDone.Set(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
Code:
static void Main(string[] args) { Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); InitConnection(client, "myserverip", myport); Send(client, "send"); sendDone.WaitOne(); Receive(client); receiveDone.WaitOne(); Console.WriteLine(response); client.Shutdown(SocketShutdown.Both); client.Close(); Console.ReadLine(); }