TCP/IP 通信
1:服務器端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;// 包含IPAddress與IPEndPoint類
using System.Threading;
namespace MyChatRoomServer
{
??? public partial class Form1 : Form
??? {
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????????? Control.CheckForIllegalCrossThreadCalls = false;
??????? }
??????? Thread watchThread = null;
??????? Socket watchSocket = null;
??????? Thread connectThread = null;
??????? private void button1_Click(object sender, EventArgs e)
??????? {
???????????? //聲明監聽套接字? 參數(使用IP4的尋址協議,使用流式連接,使用Tcp數據傳輸協議)
??????????? watchSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
??????????? //綁定端口
??????????? IPAddress ipAddress = IPAddress.Parse(this.textBox1.Text.Trim());
??????????? //創建包含ip和port的網絡節點對象
??????????? IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, int.Parse(this.textBox2.Text.Trim()));
??????????? //將監聽的隊列綁定到唯一的ip和端口上
??????????? watchSocket.Bind(ipEndpoint);
??????????? //設置監聽隊列的長度
??????????? watchSocket.Listen(10);
??????????? watchThread = new Thread(watchConnection);
??????????? watchThread.IsBackground = true;
??????????? watchThread.Start();
??????????? showMsg("服務端監聽成功!");
??????? }
??????? Dictionary<string, Socket> dict = new Dictionary<string, Socket>();
??????? Dictionary<string, Thread> dict1 = new Dictionary<string, Thread>();
??????? void watchConnection()
??????? {
??????????? while (true)//持續不斷的進行監聽
??????????? {
??????????????? //會阻塞線程
??????????????? Socket connectSocket = watchSocket.Accept();
??????????????? dict.Add(connectSocket.RemoteEndPoint.ToString(), connectSocket);
??????????????? listBox1.Items.Add(connectSocket.RemoteEndPoint.ToString());
??????????????? showMsg("客戶端連接成功! ");
??????????????? ParameterizedThreadStart td=new ParameterizedThreadStart(chart);
??????????????? Thread td1 = new Thread(td);
??????????????? dict1.Add(connectSocket.RemoteEndPoint.ToString(),td1);
??????????????? td1.Start(connectSocket);
??????????? }
??????? }
??????? private void showMsg(string message)
??????? {
??????????? this.txtMsg.AppendText(message+ "\r\n");
??????? }
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????????? string strMsg = this.txtMsgSend.Text.Trim();
??????????? byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
??????????? dict[listBox1.Text].Send(arrMsg);
??????????? showMsg("你已發送了:" + strMsg);
??????? }
??????? private void txtMsgSend_TextChanged(object sender, EventArgs e)
??????? {
??????? }
??????? void chart(object socketPara)
??????? {
??????????? Socket socket = socketPara as Socket;
??????????? while (true)
??????????? {
??????????????? byte[] arrMsgRec = new byte[1024 * 1024 * 2];
??????????????? int length = socket.Receive(arrMsgRec);
??????????????? string myMessage = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
??????????????? showMsg(myMessage);
??????????? }
??????? }
??? }
}
2:客戶端通信
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace MychatClient
{
??? public partial class FChatClient : Form
??? {
??????? public FChatClient()
??????? {
??????????? InitializeComponent();
??????????? Control.CheckForIllegalCrossThreadCalls = false;
??????? }
??????? Socket connectSocket = null;
??????? Thread connectThread = null;
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? IPAddress ipAddress = IPAddress.Parse(this.textBox1.Text.Trim());
??????????? IPEndPoint ipEndpoint = new IPEndPoint(ipAddress, int.Parse(this.textBox2.Text.Trim()));
??????????? connectSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
??????????? connectSocket.Connect(ipEndpoint);
??????????? connectThread = new Thread(chart);
??????????? connectThread.IsBackground = true;
??????????? connectThread.Start();
??????? }
??????? void chart()
??????? {
??????????? while (true)
??????????? {
??????????????? byte[] arrMsgRec = new byte[1024 * 1024 * 2];
??????????????? int length = connectSocket.Receive(arrMsgRec);
??????????????? string myMessage = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
??????????????? showMsg(myMessage);
??????????? }
??????? }
??????? private void showMsg(string message)
??????? {
??????????? this.txtMsg.AppendText(message + "\r\n");
??????? }
??????? private void button2_Click(object sender, EventArgs e)
??????? {
??????? }
??????? private void button2_Click_1(object sender, EventArgs e)
??????? {
??????????? string strMsg = this.txtMsgSend.Text.Trim();
??????????? byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
??????????? connectSocket.Send(arrMsg);
??????????? showMsg("你已發送了:" + strMsg);
??????? }
??? }
}
轉載于:https://www.cnblogs.com/wangyhua/archive/2012/08/01/4050636.html
總結
- 上一篇: Metasploit 之 webshel
- 下一篇: 用户体验思考之UI面试