自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Google GO與C#之間的TCP通信案例

開發(fā) 開發(fā)工具
Go語言是Google推出的一種全新的編程語言,具有簡潔的設(shè)計、強大的并發(fā)能力以及美好的編程體驗。

 我本人也才接觸GO兩個多月的歷史,看了幾本英文教程,讀了些Github上面的源碼,但已經(jīng)被GO的語言的簡潔和強大的并發(fā)能力所吸收,也打算繼續(xù)深入的學習,并應(yīng)用到自己的工作之中。GO語言目前主要適用于服務(wù)端的開發(fā),我參考了一些網(wǎng)絡(luò)上的教程,做了一些TCP服務(wù)端的小練習,其中服務(wù)端用GO語言開發(fā),客戶端采用C#。具體參考如下的代碼:https://github.com/yfl8910/gotcpserver

效果圖如下:

服務(wù)端代碼:

  1. package main 
  2. import ( 
  3. "net" 
  4. "fmt" 
  5.  
  6. var ( maxRead = 25 
  7.     msgStop   = []byte("cmdStop"
  8.     msgStart  = []byte("cmdContinue"
  9.     ) 
  10. func main() { 
  11.  
  12.     hostAndPort := "localhost:54321" 
  13.     listener := initServer(hostAndPort) 
  14.     for { 
  15.         conn, err := listener.Accept() 
  16.         checkError(err, "Accept: "
  17.         go connectionHandler(conn) 
  18.     } 
  19. func initServer(hostAndPort string) *net.TCPListener { 
  20.     serverAddr, err := net.ResolveTCPAddr("tcp", hostAndPort) 
  21.     checkError(err, "Resolving address:port failed: '" + hostAndPort + "'"
  22.     //listener, err := net.ListenTCP("tcp", serverAddr) 
  23.     listener, err := net.ListenTCP("tcp", serverAddr) 
  24.     checkError(err, "ListenTCP: "
  25.     println("Listening to: ", listener.Addr().String()) 
  26.     return listener 
  27. func connectionHandler(conn net.Conn) { 
  28.     connFrom := conn.RemoteAddr().String() 
  29.     println("Connection from: ", connFrom) 
  30.     talktoclients(conn) 
  31.     for { 
  32.         var ibuf []byte = make([]byte, maxRead + 1) 
  33.         length, err := conn.Read(ibuf[0:maxRead]) 
  34.         ibuf[maxRead] = 0 // to prevent overflow 
  35.     switch err { 
  36.     case nil: 
  37.         handleMsg(length, err, ibuf) 
  38.  
  39.     default
  40.         goto DISCONNECT 
  41.     } 
  42.     } 
  43.     DISCONNECT: 
  44.     err := conn.Close() 
  45.     println("Closed connection:" , connFrom) 
  46.     checkError(err, "Close:" ) 
  47.     } 
  48. func talktoclients(to net.Conn) { 
  49.     wrote, err := to.Write(msgStart) 
  50.     checkError(err, "Write: wrote " + string(wrote) + " bytes."
  51. func handleMsg(length int, err error, msg []byte) { 
  52.     if length > 0 { 
  53.         for i := 0; ; i++ { 
  54.             if msg[i] == 0 { 
  55.                 break 
  56.             } 
  57.         } 
  58.         fmt.Printf("Received data: %v"string(msg[0:length])) 
  59.         fmt.Println("   length:",length) 
  60.     } 
  61. func checkError(error error, info string) { 
  62.     if error != nil { 
  63. panic("ERROR: " + info + " " + error.Error()) // terminate program 

客戶端代碼:

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. using System.Net; 
  9. using System.Net.Sockets; 
  10. using System.Threading; 
  11.  
  12. namespace TcpClient 
  13.     public partial class Form1 : Form 
  14.     { 
  15.         private IPAddress _ipServer; //服務(wù)器IP 
  16.         private IPEndPoint _myServer; //服務(wù)器終端 
  17.         private Socket _connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//連接套接字 
  18.         private int _port; //端口 
  19.         private Thread receiveThread = null
  20.         public Form1() 
  21.         { 
  22.             InitializeComponent(); 
  23.         } 
  24.  
  25.         private bool ValidateInfo() //檢驗所填信息是否合法 
  26.         { 
  27.             if (!IPAddress.TryParse(txtbxIP.Text, out _ipServer)) 
  28.             { 
  29.                 MessageBox.Show("IP地址不合法!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
  30.                 return false
  31.             } 
  32.            if (!int.TryParse(txtbxPortNum.Text, out _port)) 
  33.             { 
  34.                 MessageBox.Show("端口號不合法!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
  35.                 return false
  36.             } 
  37.             else 
  38.             { 
  39.                 if (_port < 1024 || _port > 65535) 
  40.                 { 
  41.                     MessageBox.Show("端口號不合法!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
  42.                     return false
  43.                 } 
  44.             } 
  45.             return true
  46.         } 
  47.  
  48.  
  49.         private bool ConnectServer() //連接服務(wù)器 
  50.         {           
  51.            try 
  52.             { 
  53.                 _connectSocket.Connect(_myServer); 
  54.                 _connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(txtbxUser.Text.ToString())); 
  55.                 
  56.                 return true
  57.             } 
  58.             catch 
  59.             { 
  60.                 MessageBox.Show("服務(wù)器連接異常""錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); 
  61.                 return false
  62.             } 
  63.         } 
  64.  
  65.  
  66.         private void button1_Click(object sender, EventArgs e) 
  67.         { 
  68.             try 
  69.             {          
  70.                 _connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(comboBox1.Text.ToString()));  
  71.             } 
  72.             catch 
  73.             { 
  74.                 MessageBox.Show("服務(wù)器連接異常""錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);    
  75.             } 
  76.             
  77.         } 
  78.  
  79.         private void button3_Click(object sender, EventArgs e) 
  80.         { 
  81.           
  82.             if (!ValidateInfo()) 
  83.             { 
  84.                 return
  85.             } 
  86.             _myServer = new IPEndPoint(_ipServer, _port); 
  87.  
  88.             if (ConnectServer() == true
  89.             { 
  90.                 MessageBox.Show("連接成功!"); 
  91.  
  92.             } 
  93.         } 
  94.         private void button2_Click(object sender, EventArgs e) 
  95.         { 
  96.             this.Close(); 
  97.         } 
  98.  
  99.         private void button4_Click(object sender, EventArgs e) 
  100.         { 
  101.             for (int i = 0; i < 1000; i++) { 
  102.                 Socket _connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
  103.                 _connectSocket.Connect(_myServer); 
  104.                 _connectSocket.Send(System.Text.Encoding.UTF8.GetBytes(comboBox1.Text.ToString()+i)); 
  105.                 Thread.Sleep(2); 
  106.              
  107.             } 
  108.             
  109.             
  110.         }    
  111.  
  112.     } 

原文鏈接:http://www.cnblogs.com/yfl8910/archive/2012/12/20/2825528.html

 

【編輯推薦】

 

責任編輯:彭凡 來源: 博客園
相關(guān)推薦

2009-09-10 11:26:59

C# form

2009-08-24 17:20:13

C#網(wǎng)絡(luò)通信TCP連接

2011-03-10 09:07:47

liferayportlet

2009-11-11 10:43:49

Go語言Google

2012-12-24 14:40:54

iosjs

2021-12-16 16:20:57

GoWebSocketLinux

2011-07-18 09:47:20

ModBusC#

2024-12-24 07:38:44

C#串口通信

2010-02-01 13:08:46

C++函數(shù)指針C#托

2010-03-18 19:06:35

Java socket

2009-08-13 16:27:07

C#基于TCP協(xié)議

2009-08-28 15:35:31

C#與VB.net

2009-09-04 15:57:49

C#實現(xiàn)漢字之間互換

2009-08-28 10:08:02

C#數(shù)值類型之間轉(zhuǎn)換

2024-12-26 14:48:46

C#Modbus通信

2010-03-09 10:59:42

Python語言教程

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2015-03-03 13:47:34

HttpTCPIP

2009-08-21 15:59:22

服務(wù)端與客戶端通信

2024-04-29 06:39:45

WebSocketSocketC#
點贊
收藏

51CTO技術(shù)棧公眾號