學(xué)習(xí)socket 的一些筆記,希望和大家一起討論 。
一.什么是socket
所謂socket通常也稱(chēng)作"套接字",應(yīng)用程序通常通過(guò)"套接字"向網(wǎng)絡(luò)發(fā)出請(qǐng)求或者應(yīng)答網(wǎng)絡(luò)請(qǐng)求。 以J2SDK-1.3為例,Socket和ServerSocket類(lèi)庫(kù)位于java.net包中。ServerSocket用于服務(wù)器端,Socket是建立網(wǎng)絡(luò)連接時(shí)使用的。在連接成功時(shí),應(yīng)用程序兩端都會(huì)產(chǎn)生一個(gè)Socket實(shí)例,操作這個(gè)實(shí)例,完成所需的會(huì)話(huà)。對(duì)于一個(gè)網(wǎng)絡(luò)連接來(lái)說(shuō),套接字是平等的,并沒(méi)有差別,不因?yàn)樵诜?wù)器端或在客戶(hù)端而產(chǎn)生不同級(jí)別。不管是Socket還是ServerSocket它們的工作都是通過(guò)SocketImpl類(lèi)及其子類(lèi)完成的。
二 .開(kāi)發(fā)原理:
服務(wù)器,使用ServerSocket監(jiān)聽(tīng)指定的端口,端口可以隨意指定(由于1024以下的端口通常屬于保留端口,在一些操作系統(tǒng)中不可以隨意使用,所以建議使用大于1024的端口),等待客戶(hù)連接請(qǐng)求,客戶(hù)連接后,會(huì)話(huà)產(chǎn)生;在完成會(huì)話(huà)后,關(guān)閉連接??蛻?hù)端,使用Socket對(duì)網(wǎng)絡(luò)上某一個(gè)服務(wù)器的某一個(gè)端口發(fā)出連接請(qǐng)求,一旦連接成功,打開(kāi)會(huì)話(huà);會(huì)話(huà)完成后,關(guān)閉Socket??蛻?hù)端不需要指定打開(kāi)的端口,通常臨時(shí)的、動(dòng)態(tài)的分配一個(gè)1024以上的端口。Socket接口是TCP/IP網(wǎng)絡(luò)的API,Socket接口定義了許多函數(shù)或例程,程序員可以用它們來(lái)開(kāi)發(fā)TCP/IP網(wǎng)絡(luò)上的應(yīng)用程序。要學(xué)Internet上的TCP/IP網(wǎng)絡(luò)編程,必須理解Socket接口。Socket接口設(shè)計(jì)者最先是將接口放在Unix操作系統(tǒng)里面的。如果了解Unix系統(tǒng)的輸入和輸出的話(huà),就很容易了解Socket了。網(wǎng)絡(luò)的Socket數(shù)據(jù)傳輸是一種特殊的I/O,Socket也是一種文件描述符。Socket也具有一個(gè)類(lèi)似于打開(kāi)文件的函數(shù)調(diào)用Socket(),該函數(shù)返回一個(gè)整型的Socket描述符,隨后的連接建立、數(shù)據(jù)傳輸?shù)炔僮鞫际峭ㄟ^(guò)該Socket實(shí)現(xiàn)的。
三.簡(jiǎn)單的socket 同步代碼,目前代碼沒(méi)有上服務(wù)器測(cè)試,不知道同步和異步的效果怎么樣!
需求
1.客戶(hù)端是公司內(nèi)部的機(jī)器200臺(tái)計(jì)算機(jī)。
2.可以同時(shí)連接,長(zhǎng)連接。
3.服務(wù)器接受到客戶(hù)端發(fā)來(lái)的信息,發(fā)送 socket數(shù)據(jù) 發(fā)送給總公司socket服務(wù)器 ,總公司返回?cái)?shù)據(jù)。
socket 同步簡(jiǎn)單代碼
代碼 public class Server
{
static void Main(string[] args)
{
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
//創(chuàng)建 socket Tcp
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Socket connection = listener.Accept();
SocketProxy sp = new SocketProxy(connection);
Thread thread = new Thread(new ThreadStart(sp.TcpSendData));
thread.Name = connection.RemoteEndPoint.ToString();
thread.Start();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
}
public class SocketProxy
{
private Socket _socket;
public SocketProxy(Socket connection)
{
this._socket = connection;
}
public void TcpSendData()
{
byte[] bytes;
string data = string.Empty;
while (true)
{
bytes = new byte[1024];
// Receive client data
int bytesRec = this._socket.Receive(bytes);
// convert bytes to string
data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
Console.WriteLine(data);
}
}
}
異步代碼
internal class SocketServer
{
static void Main(string[] args)
{
SocketTcpListener listener = new SocketTcpListener();
listener.StartListening();
}
}
internal class SocketTcpListener
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
//開(kāi)始監(jiān)聽(tīng)
public void StartListening()
{
//Data buffer for incoming data.
byte[] bytes = new Byte[1024];
//Establish the local endpoint for the socket.
//The DNS name of the computer
//running the listener is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 9050);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection");
StateObject state = new StateObject();
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
//異步接受
private static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
Array.Clear(state.buffer, 0, state.buffer.Length); // 清空緩存,避免臟讀
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
//異步讀取
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
String resultdata = string.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.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));
// Check for end-of-file tag. If it is not there, read
// more data.
content = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
Array.Clear(state.buffer, 0, state.buffer.Length); // 清空緩存,避免臟讀
Console.WriteLine(content);
if (content.Length > -1)
{
// All the data has been read from the
// client. Display it on the console.
//Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
// Echo the data back to the client.
//SocketTcpClient.CreateSocketObjest();
// get caac data
// 0 如果是第一次,需要?jiǎng)?chuàng)建socket對(duì)象,發(fā)送登陸信息,匹配出用戶(hù)名(<name,socket>),存放到集合
// 1.如果用戶(hù)存在,需要使用用戶(hù)名來(lái)匹配socket對(duì)象,如果沒(méi)有連接配置,需要?jiǎng)?chuàng)建socket對(duì)象,保存到集合以便下次使用連接對(duì)象,避免再次的登陸
// 2.發(fā)送數(shù)據(jù)完,遠(yuǎn)程服務(wù)器會(huì)返回信息給Socket Server
// 3.Socket Server 返回信息給 Clinet
state.ResultData = content;
state.SocketName = "Jackyong";
resultdata =SocketTcpClient.Send(handler, content);
// send client
//Send(handler, resultdata);
Send(state);
}
//else
//{
// // Not all data received. Get more.
// handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);
//}
}
}
//發(fā)送數(shù)據(jù)
private static void Send(StateObject state)
{
Array.Clear(state.buffer, 0, state.buffer.Length); // 清空緩存,避免臟讀
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(state.ResultData);
// Begin sending the data to the remote device.
state.workSocket.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), state);
}
// 異步發(fā)送數(shù)據(jù)給client
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
// Socket handler = (Socket)ar.AsyncState;
StateObject state = (StateObject)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = state.workSocket.EndSend(ar);
// if detect exceptoin socket shut down /close
// handler.Shutdown(SocketShutdown.Both);
// handler.Close();
// continue to listener
//StateObject state = new StateObject();
//state.workSocket = handler;
Array.Clear(state.buffer, 0, state.buffer.Length); // 清空緩存,避免臟讀
state.workSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
internal class SocketTcpClient
{
/// <summary>
/// create socket object ,client send server
/// </summary>
/// <param name="command">command</param>
/// <returns>data</returns>
public static Socket CreateSocketObjest()
{
//需要存儲(chǔ)這個(gè)連接對(duì)象
string receivemsg = string.Empty;
byte[] data = new byte[1024];
string IP = "127.0.0.1";
int Port = 9050;
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(IP), Port);//服務(wù)器的IP和端口
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
//因?yàn)榭蛻?hù)端只是用來(lái)向特定的服務(wù)器發(fā)送信息,所以不需要綁定本機(jī)的IP和端口。不需要監(jiān)聽(tīng)。
client.Connect(ie);
}
catch (SocketException e)
{
//close socket and writing error exception
Console.WriteLine(e.ToString());
}
return client;
}
/// <summary>
/// client send command server
/// </summary>
/// <param name="client">socket</param>
/// <param name="command">command</param>
/// <returns>data</returns>
public static string Send(Socket client, string command)
{
byte[] data = new byte[1024];
string result = string.Empty;
//send command
ClientSendDataServer(client, command);
if (client.Poll(-1, SelectMode.SelectRead))
{ //如果傳遞過(guò)來(lái)的是string.empty 字符串也是0
int recv = client.Receive(data);
if (recv == 0)
{
//socket連接已斷開(kāi)
}
result = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(result);
}
return result;
}
/// <summary>
/// client to server
/// </summary>
/// <param name="handler">socket </param>
/// <param name="data">comand</param>
private static void ClientSendDataServer(Socket client, String command)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(command);
// Begin sending the data to the remote device.
client.Send(byteData);
}
}
/// <summary>
/// 存放客戶(hù)端與總公司的登陸信息
/// </summary>
internal class SocketUser
{
private System.Collections.Generic.Dictionary<string, Socket> _sockets;
public SocketUser()
{
_sockets = new Dictionary<string, Socket>();
}
/// <summary>
/// select client user
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Socket Find(string key)
{
Socket socket = null; ;
if (key == null || key == string.Empty)
{
}
if (_sockets[key] != null)
{
socket = _sockets[key] as Socket;
}
return socket;
}
/// <summary>
/// add client in sockets
/// </summary>
/// <param name="key"></param>
/// <param name="sk"></param>
public void Add(string key, Socket sk)
{
_sockets.Add(key, sk);
}
/// <summary>
/// Remove client user
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
_sockets.Remove(key);
}
}
// socket state
internal class StateObject
{
// socket name
public string SocketName { get; set; }
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public string ResultData { get; set; }
}