[C#]
public static void Connect(EndPoint remoteEP, Socket client) {client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client );
connectDone.WaitOne();
}
[C#]
private static void ConnectCallback(IAsyncResult ar) {try {// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
} catch (Exception e) {Console.WriteLine(e.ToString());
}
}
[C#]
private static void Send(Socket client, String data) {// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(SendCallback), client);
}
[C#]
private static void SendCallback(IAsyncResult ar) {try {// Retrieve the socket from the state object.
Socket client = (Socket) ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);// Signal that all bytes have been sent.
sendDone.Set();
} catch (Exception e) {Console.WriteLine(e.ToString());
}
}
[C#]
public class StateObject {public Socket workSocket = null;// Client socket.
public const int BufferSize = 256;// Size of receive buffer.
public byte[] buffer = new byte[BufferSize];// Receive buffer.
public StringBuilder sb = new StringBuilder();// Received data string.
}
[C#]
private static void Receive(Socket client) {try {// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
} catch (Exception e) {Console.WriteLine(e.ToString());
}
}
[C#]
private static void ReceiveCallback( IAsyncResult ar ) {try {// Retrieve the state object and the client socket
// from the async state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
//Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {// All the data has arrived; put it in response.
if (state.sb.Length > 1) {response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {Console.WriteLine(e.ToString());
}
}
[C#]
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
[C#]
listener.Bind(localEndPoint);
listener.Listen(100);
[C#]
Console.WriteLine("Waiting for a connection...");Socket handler = listener.Accept();
String data = null;
while (true) {bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes,0,bytesRec);
if (data.IndexOf("<EOF>") > -1) {break;
}
}
Console.WriteLine( "Text received : {0}", data);byte[] msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
[C#]
void acceptCallback( IAsyncResult ar) {//Add the callback code here.
}
[C#]
listener.BeginAccept(
new AsyncCallback(SocketListener.acceptCallback),
listener);
[C#]
public void StartListening() {IPHostEntry ipHostInfo = new Dns.Resolve(Dns.GetHostName());
IPEndPoint localEP = new IPEndPoint(ipHostInfo.AddressList[0],11000);
Console.WriteLine("Local address and port : {0}",localEP.ToString());Socket listener = new Socket( localEP.Address.AddressFamily,
SocketType.Stream, ProtocolType.Tcp );
try {listener.Bind(localEP);
s.Listen(10);
while (true) {allDone.Reset();
Console.WriteLine("Waiting for a connection...");listener.BeginAccept(
new AsyncCallback(SocketListener.acceptCallback),
listener );
allDone.WaitOne();
}
} catch (Exception e) {Console.WriteLine(e.ToString());
}
Console.WriteLine( "Closing the listener...");
}
[C#]
public void acceptCallback(IAsyncResult ac) {allDone.Set();
Socket listener = (Socket) ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Additional code to read data goes here.
}
[C#]
public class StateObject {public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();<>
……