C# Socket

using UnityEngine;

using System.Collections;

using System.Net;

using System.Net.Sockets;

using System;

using System.Text;

public class ClientAsyncSocket : MonoBehaviour {

 

 

 

/// <summary>

/// Ayncs the connect socket.

/// </summary>

/// <returns>

/// The connect socket.

/// </returns>

/// <param name='server'>

/// Server:""http://113.11.205.94"

/// </param>

/// <param name='port'>

/// Port:80

/// </param>

 

private static void ayncConnectSocket(string server,int port){

 

//   Version 0

//IPHostEntry    hostEntry = null;

//

//   

//        hostEntry = Dns.GetHostEntry(server);

//

//   foreach(IPAddress address in hostEntry.AddressList)

//        {

//            IPEndPoint ipe = new IPEndPoint(address, port);

//            Socket tempSocket = 

//                new Socket(ipe.AddressFamily, socketType, protocolType);

//tempSocket.BeginConnect(ipe,new AsyncCallback(ConnectCallback),tempSocket);

//

//        }

//   Version 0

 

IPHostEntry    hostEntry=Dns.GetHostEntry(server);

IPAddress address=hostEntry.AddressList[0];

IPEndPoint ipe = new IPEndPoint(address, port);

Socket s=new Socket(ipe.AddressFamily,SocketType.Stream,ProtocolType.Tcp);

    }

 

private static void ConnectCallback(IAsyncResult ar)

{

try {

Sockets=(Socket)ar.AsyncState;

s.EndConnect(ar);

 

catch (Exception ex) {

Debug.Log(ex.ToString());

}

 

}

/// <summary>

/// Asyncs the socket send.

/// </summary>

/// <param name='client'>

/// Client:Socket 

/// </param>

/// <param name='data'>

/// Data: Send Data

/// </param>

private static void asyncSocketSend(Socket client,string  data)

{

try {

// 譬シ蠑剰スャ謐「.  

byte[] byteData = Encoding.ASCII.GetBytes(data);  

 

 

client.BeginSend(byteData, 0, byteData.Length, 0,  

new AsyncCallback(SendCallback), client);  

 

catch (Exception ex) {

Debug.Log(ex.ToString());

}

 

 

}

private static void SendCallback(IAsyncResult ar)  

{   

try{

// 莉市tate蟇ケ雎。荳ュ闔キ蜿穆ocket  

Socket client = (Socket)ar.AsyncState;  

 

 

int bytesSent = client.EndSend(ar);  

Debug.Log("Sent {0} bytes to server."+ bytesSent);  

 

catch (Exception e) {

Debug.Log(e.ToString());

}  

}

 

private static void ayncSocketReceive(Socket client){

try{

 

StateObject state = new StateObject();  

state.workSocket = client;  

 

// 莉手ソ懃ィ狗岼譬㍽磁謾カ謨ー謐ョ.  

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,  

new AsyncCallback(ReceiveCallback), state);  

catch (Exception e) {

Console.WriteLine(e.ToString());

}

}

 

privatestaticstring response=string.Empty;

 

private static void ReceiveCallback(IAsyncResult ar)  

{   

try{

 

// 莉手セ灘Ⅶ蜿よ焚蠑よュ・state蟇ケ雎。荳ュ闔キ蜿穆tate蜥茎ocket蟇ケ雎。  

StateObject state = (StateObject)ar.AsyncState;  

Socket client = state.workSocket;  

 

//莉手ソ懃ィ玖ョセ螟㍻ッサ蜿匁焚謐ョ  

int bytesRead = client.EndReceive(ar);  

 

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();  

}   

 

}   

catch (Exception e) {

Debug.Log(e.ToString());

}

}

 

}

public class StateObject {

// Client socket.

public Socket workSocket = null;

// Size of receive buffer.

public const int BufferSize = 256;

// Receive buffer.

public byte[] buffer = new byte[BufferSize];

// Received data string.

public StringBuilder sb = new StringBuilder();

}

 

posted @ 2013-08-26 13:28  韦斯利yx  阅读(399)  评论(0编辑  收藏  举报