Hi,
I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated.
I have a server which listens, receives calls, processes them and sends back the results to clients.
The code below makes the client application not to respond. Client can send data but is stuck in the process of waiting information back from the server.
Any ideas?
I'm stuck on a very simple problem and just cant seem to get around it, little help would be much appreciated.
I have a server which listens, receives calls, processes them and sends back the results to clients.
The code below makes the client application not to respond. Client can send data but is stuck in the process of waiting information back from the server.
Any ideas?
Code:
using System; using System.Net.Sockets; using System.Collections.Generic; using System.IO; using Joker.CardElem; using System.Data; using System.ComponentModel; using System.Text; using System.Threading; namespace Joker { public class AsynchNetworkServer { class ClientHandler { public ClientHandler(Socket socketForClient) { socket = socketForClient; buffer = new byte[256]; networkStream = new NetworkStream(socketForClient); callbackRead = new AsyncCallback(this.OnReadComplete); callbackWrite = new AsyncCallback(this.OnWriteComplete); } public void StartRead() { networkStream.BeginRead(buffer, 0, buffer.Length, callbackRead, null); } private void OnReadComplete(IAsyncResult ar) { int bytesRead = networkStream.EndRead(ar); if (bytesRead > 0) { string s = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead); Console.Write("Received {0} bytes from client: {1}" , bytesRead, s); string[] level = s.Split(' '); string quest = calcNext(int.Parse(level[0]), int.Parse(level[1])).ToString(); networkStream.BeginWrite(Encoding.ASCII.GetBytes(quest), 0, Encoding.ASCII.GetBytes(quest).Length, OnWriteComplete, networkStream); } else { Console.WriteLine("Read connection dropped"); networkStream.Close(); socket.Close(); networkStream = null; socket = null; } } private void OnWriteComplete(IAsyncResult ar) { networkStream.EndWrite(ar); Console.WriteLine("Write complete"); networkStream.BeginRead(buffer, 0, buffer.Length,callbackRead, null); } private byte[] buffer; private Socket socket; private NetworkStream networkStream; private AsyncCallback callbackRead; private AsyncCallback callbackWrite; private int pNum=0; private JGame game = new JGame(); } public static void Main() { AsynchNetworkServer app = new AsynchNetworkServer(); app.Run(); } private void Run() { // create a new TcpListener and start it up // listening on port 65000 TcpListener tcpListener = new TcpListener(8227); tcpListener.Start(); Console.WriteLine("Listening For Clients!"); // keep listening until you send the file for (; ; ) { // if a client connects, accept the connection // and return a new socket named socketForClient // while tcpListener keeps listening Socket socketForClient = tcpListener.AcceptSocket(); if (socketForClient.Connected) { Console.WriteLine("Client connected"); ClientHandler handler = new ClientHandler(socketForClient); handler.StartRead(); } } } }
Comment