none
socket服务器端的设计 RRS feed

  • 问题

  • using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; namespace 道路交通监测信息系统 { class SocketConnection { public int myPort = 3000;//设置端口号 public Thread thread; public Socket socket; Socket[] socConnection = new Socket[12]; public int clientNum = 0; public void StartListening() { try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); IPEndPoint endPoint = new IPEndPoint(ipaddress, myPort); socket.Bind(endPoint); socket.Listen(20); thread = new Thread(new ThreadStart(WatchConnection)); thread.IsBackground = true; thread.Start(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("连接中....." + ex.Message); } } private void WatchConnection() { while (true) { socConnection[clientNum] = socket.Accept(); Thread thread = new Thread(new ParameterizedThreadStart(ServerRecMsg)); thread.IsBackground = true; thread.Start(socConnection[clientNum]); clientNum++; } } private void ServerRecMsg(object socketClientPara) { Socket socketServer = socketClientPara as Socket; try { Label: while (true) { byte[] arrServerRecMsg = new byte[1024 * 1024]; int length = socketServer.Receive(arrServerRecMsg); string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); if (strSRecMsg != null) { string str = strSRecMsg; string RoadMonitorID = "0"; .......

    string sql=.....

    DbHelperSQL.ExecuteSql(sql); }

    else if

    goto Label; } } catch (Exception ex) { } } }

    然后我在在其他类中调用它,这么写的:

    SocketConnection socketCon = new SocketConnection();

    socketCon.StartListening();

    但问题是那一段socket的代码并没有执行

    2016年6月8日 6:56

答案

全部回复