日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀]最近有段時(shí)間因?yàn)橛袀€(gè)項(xiàng)目一直需要Socket數(shù)據(jù)傳輸。所以呢沒辦法就整了一套Socket最簡(jiǎn)單的版本出來。以便自己日后方便使用。我遇到的程序Socket是通過Tcp Ip的方式進(jìn)行數(shù)據(jù)傳輸?shù)?不多說先

最近有段時(shí)間因?yàn)橛袀€(gè)項(xiàng)目一直需要Socket數(shù)據(jù)傳輸。所以呢沒辦法就整了一套Socket最簡(jiǎn)單的版本出來。以便自己日后方便使用。

我遇到的程序Socket是通過Tcp Ip的方式進(jìn)行數(shù)據(jù)傳輸?shù)?

不多說先上圖



當(dāng)輸入IP跟端口之后進(jìn)行連接button事件:

? ? ? ? private void Open_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (IP.Text != "" && Port.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Open.Enabled = false;
? ? ? ? ? ? ? ? Close.Enabled = true;
? ? ? ? ? ? ? ? //實(shí)例化 套接字
? ? ? ? ? ? ? ? sokClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? ? ? ? ? //創(chuàng)建 ip對(duì)象
? ? ? ? ? ? ? ? IPAddress address = IPAddress.Parse(IP.Text.Trim());
? ? ? ? ? ? ? ? //創(chuàng)建網(wǎng)絡(luò)節(jié)點(diǎn)對(duì)象 包含 ip和port
? ? ? ? ? ? ? ? IPEndPoint endpoint = new IPEndPoint(address, int.Parse(Port.Text.Trim()));
? ? ? ? ? ? ? ? //連接 服務(wù)端監(jiān)聽套接字
? ? ? ? ? ? ? ? sokClient.Connect(endpoint);


? ? ? ? ? ? ? ? //創(chuàng)建負(fù)責(zé)接收 服務(wù)端發(fā)送來數(shù)據(jù)的 線程
? ? ? ? ? ? ? ? threadClient = new Thread(ReceiveMsg);
? ? ? ? ? ? ? ? threadClient.IsBackground = true;
? ? ? ? ? ? ? ? //如果在win7下要通過 某個(gè)線程 來調(diào)用 文件選擇框的代碼,就需要設(shè)置如下
? ? ? ? ? ? ? ? threadClient.SetApartmentState(ApartmentState.STA);
? ? ? ? ? ? ? ? threadClient.Start();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("請(qǐng)將端口跟IP地址填寫完整!");
? ? ? ? ? ? }
? ? ? ? }

連接上之后填寫內(nèi)容點(diǎn)擊發(fā)送按鈕button事件如下:

? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (txtInput.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string Timer = DateTime.Now.ToString(); ? ? ? ? ? ?// 2008-9-4 20:02:10
? ? ? ? ? ? ? ? txtShow.AppendText("Client " + Timer + ":" + txtInput.Text.Trim() + "rn");
? ? ? ? ? ? ? ? byte[] arrMsg = System.Text.Encoding.Default.GetBytes(txtInput.Text.Trim() + "rn");//這里可以根據(jù)客戶的需求進(jìn)行格式的轉(zhuǎn)換
? ? ? ? ? ? ? ? sokClient.Send(arrMsg);
? ? ? ? ? ? }


? ? ? ? }

結(jié)束數(shù)據(jù)端的部分是使用的線程處理的方式進(jìn)行監(jiān)聽。當(dāng)數(shù)據(jù)過來之后進(jìn)行數(shù)據(jù)處理為:

? ? ? ? void ReceiveMsg()
? ? ? ? {
? ? ? ? ? ? ? ? while (isRec)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? byte[] msgArr = new byte[1024 * 1024 * 1];//接收到的消息的緩沖區(qū)
? ? ? ? ? ? ? ? ? ? int length = 0;
? ? ? ? ? ? ? ? ? ? //接收服務(wù)端發(fā)送來的消息數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? length = sokClient.Receive(msgArr);//Receive會(huì)阻斷線程
? ? ? ? ? ? ? ? ? ? ? ? string strMsg = System.Text.Encoding.Default.GetString(msgArr, 0, length);//這里可以根據(jù)客戶的需求進(jìn)行格式的轉(zhuǎn)換
? ? ? ? ? ? ? ? ? ? ? ? if (strMsg != "")//發(fā)送來的是文字
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? string Timer = DateTime.Now.ToString(); ? ? ? ? ? ?// 2008-9-4 20:02:10
? ? ? ? ? ? ? ? ? ? ? ? ? ? // string strMsg = System.Text.Encoding.UTF8.GetString(msgArr, 1, length - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Invoke(new Action(delegate { txtShow.AppendText("Server " + Timer + ":" + strMsg + "rn"); }));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch { }
? ? ? ? ? ? ? ? }
? ? ? ? }


大體的主要代碼就是這樣了,好了。將所有的代碼都貼一下方便大家使用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Socket_Client
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? Socket sokClient = null;//負(fù)責(zé)與服務(wù)端通信的套接字
? ? ? ? Thread threadClient = null;//負(fù)責(zé) 監(jiān)聽 服務(wù)端發(fā)送來的消息的線程
? ? ? ? bool isRec = true;//是否循環(huán)接收服務(wù)端數(shù)據(jù)


? ? ? ? void ReceiveMsg()
? ? ? ? {
? ? ? ? ? ? ? ? while (isRec)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? byte[] msgArr = new byte[1024 * 1024 * 1];//接收到的消息的緩沖區(qū)
? ? ? ? ? ? ? ? ? ? int length = 0;
? ? ? ? ? ? ? ? ? ? //接收服務(wù)端發(fā)送來的消息數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? length = sokClient.Receive(msgArr);//Receive會(huì)阻斷線程
? ? ? ? ? ? ? ? ? ? ? ? string strMsg = System.Text.Encoding.Default.GetString(msgArr, 0, length);
? ? ? ? ? ? ? ? ? ? ? ? if (strMsg != "")//發(fā)送來的是文字
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? string Timer = DateTime.Now.ToString(); ? ? ? ? ? ?// 2008-9-4 20:02:10
? ? ? ? ? ? ? ? ? ? ? ? ? ? // string strMsg = System.Text.Encoding.UTF8.GetString(msgArr, 1, length - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Invoke(new Action(delegate { txtShow.AppendText("Server " + Timer + ":" + strMsg + "rn"); }));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch { }
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }


? ? ? ? private void Open_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (IP.Text != "" && Port.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Open.Enabled = false;
? ? ? ? ? ? ? ? Close.Enabled = true;
? ? ? ? ? ? ? ? //實(shí)例化 套接字
? ? ? ? ? ? ? ? sokClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
? ? ? ? ? ? ? ? //創(chuàng)建 ip對(duì)象
? ? ? ? ? ? ? ? IPAddress address = IPAddress.Parse(IP.Text.Trim());
? ? ? ? ? ? ? ? //創(chuàng)建網(wǎng)絡(luò)節(jié)點(diǎn)對(duì)象 包含 ip和port
? ? ? ? ? ? ? ? IPEndPoint endpoint = new IPEndPoint(address, int.Parse(Port.Text.Trim()));
? ? ? ? ? ? ? ? //連接 服務(wù)端監(jiān)聽套接字
? ? ? ? ? ? ? ? sokClient.Connect(endpoint);


? ? ? ? ? ? ? ? //創(chuàng)建負(fù)責(zé)接收 服務(wù)端發(fā)送來數(shù)據(jù)的 線程
? ? ? ? ? ? ? ? threadClient = new Thread(ReceiveMsg);
? ? ? ? ? ? ? ? threadClient.IsBackground = true;
? ? ? ? ? ? ? ? //如果在win7下要通過 某個(gè)線程 來調(diào)用 文件選擇框的代碼,就需要設(shè)置如下
? ? ? ? ? ? ? ? threadClient.SetApartmentState(ApartmentState.STA);
? ? ? ? ? ? ? ? threadClient.Start();
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("請(qǐng)將端口跟IP地址填寫完整!");
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? private void Close_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Open.Enabled = true;
? ? ? ? ? ? Close.Enabled = false;
? ? ? ? ? ? sokClient.Close();
? ? ? ? }


? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (txtInput.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string Timer = DateTime.Now.ToString(); ? ? ? ? ? ?// 2008-9-4 20:02:10
? ? ? ? ? ? ? ? txtShow.AppendText("Client " + Timer + ":" + txtInput.Text.Trim() + "rn");
? ? ? ? ? ? ? ? byte[] arrMsg = System.Text.Encoding.Default.GetBytes(txtInput.Text.Trim() + "rn");
? ? ? ? ? ? ? ? sokClient.Send(arrMsg);
? ? ? ? ? ? }


? ? ? ? }


? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Close.Enabled = false;
? ? ? ? }
? ? }
}

運(yùn)行結(jié)果如下:










本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉