Ò»¡¢TCP¿Í»§¶ËÓë·þÎñÆ÷¶ËͨÐÅ
·þÎñÆ÷³õʼ»¯
(1)µ÷ÓÃsocket,´´½¨ÎļþÃèÊö·û (2)µ÷ÓÃbind,½«ÎļþÃèÊö·ûÓëip/portÁ¬½ÓÆðÀ´¡£Èô¶Ë¿ÚºÅÒѱ»Õ¼ÓÃ,Ôòbindʧ°Ü (3)µ÷ÓÃlisten,ÉùÃ÷¸ÃÎļþÃèÊö·ûÊÇ·þÎñÆ÷µÄÒ»¸öÎļþÃèÊö·û,Ϊ֮ºóµÄaccept×ö×¼±¸ (4)µ÷ÓÃaccept,²¢´¦ÓÚ×èÈû״̬,µÈ´ý¿Í»§¶ËÁ¬½Ó
½¨Á¢Á¬½Ó
(1)µ÷ÓÃsocket,´´½¨ÎļþÃèÊö·û (2)µ÷ÓÃconnect,Ïò·þÎñÆ÷·¢ÆðÁ¬½ÓÇëÇó¡£ (3)connect»á·¢ËÍÒ»¸öÇëÇóSYN¶Î²¢×èÈûµÈ´ý·þÎñÆ÷Ó¦´ð(µÚÒ»´Î) (4)·þÎñÆ÷ÊÕµ½SYN,»á¸ø¿Í»§¶Ë·¢ËÍÒ»¸öÈ·ÈÏÓ¦´ðµÄͬʱ·¢ËÍÒ»¸öÇëÇó(SYN+ACK),±íʾͬÒ⽨Á¢Á¬½Ó(µÚ¶þ´Î) (5)¿Í»§¶ËÊÕµ½¿Í»§¶Ë·¢µÄSYN+ACK¶Î,±íÃ÷¿Í»§¶ËÁ¬½ÓÒѽ¨Á¢³É¹¦,½øÈëESTABLISHED״̬,´Óconnect()¡£¿Í»§¶ËÔÙÏò·þÎñÆ÷·¢ËÍÒ»¸öACK¶Î,·þÎñÆ÷ÊÕµ½ºóÔò·þÎñÆ÷¶ËÁ¬½ÓÒ²½¨Á¢³É¹¦,·þÎñÆ÷Ò²½øÈëESTABLISHED״̬¡£
Êý¾Ý´«Êä
(1)Á¬½Ó½¨Á¢³É¹¦ºó,ÔÚͬһÁ¬½Ó¡¢Í¬Ò»Ê±¿Ì,ͨÐÅË«·½¿ÉͬʱдÊý¾Ý(È«Ë«¹¤) (2)·þÎñÆ÷¶Ë´Óaccept()·µ»Øºóµ÷ÓÃread()¿ªÊ¼¶ÁÊý¾Ý,ÈôûÓÐÊý¾ÝÔò×èÈûµÈ´ý (3)¿Í»§¶Ëµ÷ÓÃwrite()Ïò·þÎñÆ÷·¢ËÍÊý¾ÝÇëÇó,¿Í»§¶ËÊÕµ½Ö®ºóµ÷ÓÃread()´¦ÀíÇëÇó,´Ë¹ý³Ì·þÎñÆ÷µ÷ÓÃread()×èÈûµÈ´ý (4)·þÎñÆ÷µ÷ÓÃwrite()½«´¦ÀíºÃµÄÇëÇó·¢Ë͸ø¿Í»§¶Ë,Ôٴε÷ÓÃread()µÈ´ýÏÂÒ»¸öÇëÇó (5)¿Í»§¶ËÊÕµ½ºó´Óread()·µ»Ø,·¢ËÍÏÂÒ»ÌõÇëÇó,Èç´ËÑ»·ÏÂÈ¥
¶Ï¿ªÁ¬½Ó
(1)ûÓÐÊý¾Ý´¦ÀíÁË,Ôò¿Í»§¶Ëµ÷ÓÃclose()¹Ø±ÕÁ¬½Ó,¸ø·þÎñÆ÷¶Ë·¢ËÍÒ»¸ö¶Ï¿ªÁ¬½ÓÇëÇóFIN¶Î(µÚÒ»´Î) (2)·þÎñÆ÷ÊÕµ½¿Í»§¶ËµÄFIN¶Î,¸ø¿Í»§¶Ë·¢ËÍÒ»¸öÈ·ÈÏÓ¦´ðACK¶Î±íÃ÷ͬÒâ¶Ï¿ªÁ¬½Ó,¿Í»§¶ËÊÕµ½ACK¶Î²¢µ÷ÓÃread()·µ»Ø0,±íÃ÷¿Í»§¶ËÁ¬½ÓÒѶϿª(µÚ¶þ´Î) (3)read()·µ»Ø0Ö®ºó,·þÎñÆ÷ÖªµÀ¿Í»§¶ËÒѶϿªÁ¬½Ó,ËüÒ²µ÷ÓÃclose()¹Ø±ÕÁ¬½Ó,¸ø¿Í»§¶Ë·¢ËÍÒ»¸ö¶Ï¿ªÁ¬½ÓÇëÇóFIN¶Î(µÚÈý´Î) (4)¿Í»§¶Ë½ÓÊÕµ½·þÎñÆ÷¶Ë·¢Ë͵ÄFIN¶Î,¸ø·þÎñÆ÷Ò»¸öÈ·ÈÏÓ¦´ðACK¶Î,±íÃ÷ͬÒâ¶Ï¿ªÁ¬½Ó¡£¿Í»§¶Ë½øÈëTIME_WAIT״̬,·þÎñÆ÷ÊÕµ½¿Í»§¶ËµÄACKºóÔò·þÎñÆ÷¶Ï¿ªÁ¬½Ó¡£
¶þ¡¢±àдÍøÓοͻ§¶Ë
- ´´½¨ÏîÄ¿
- »ù±¾½çÃæÉè¼Æ
- ´úÂë
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Media;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private NetworkStream stream;
private TcpClient tcpClient;
SoundPlayer player = new SoundPlayer("D:/Fuchikami/nene/Lopu$ - So Cute~.mp3");
Socket socket_send;
void send_stream(string str)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
byte[] buffer = GBK.GetBytes(str + "\n");
stream.Write(buffer, 0, buffer.Length);
}
void receive_stream()
{
byte[] receive_data = new byte[1024];
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
System.Text.Encoding GBK = System.Text.Encoding.GetEncoding("GBK");
if (stream.CanRead)
{
int len = stream.Read(receive_data, 0, receive_data.Length);
string msg = GBK.GetString(receive_data, 0, receive_data.Length);
string str = "\r\n";
char[] str1 = str.ToCharArray();
string[] messy_code = { "ÿ?ÿ?[2J ", "[5m", "[44m", "[37;0m", "[1;33m", "[1;32m", "[1;31m" };
string[] msg1 = msg.Split(str1);
for (int j = 0; j < msg1.Length; j++)
{
msg1[j] = msg1[j].Replace(messy_code[0], " ");
msg1[j] = msg1[j].Replace(messy_code[1], " ");
msg1[j] = msg1[j].Replace(messy_code[2], " ");
msg1[j] = msg1[j].Replace(messy_code[3], " ");
msg1[j] = msg1[j].Replace(messy_code[4], " ");
msg1[j] = msg1[j].Replace(messy_code[5], " ");
msg1[j] = msg1[j].Replace(messy_code[6], " ");
listBox1.Items.Add(msg1[j]);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient();
socket_send = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint point = new IPEndPoint(IPAddress.Parse("10.1.230.74"), 3900);
socket_send.Connect(point);
try
{
tcpClient.Connect("10.1.230.74", 3900);
listBox1.Items.Add("Á¬½Ó³É¹¦");
stream = tcpClient.GetStream();
receive_stream();
}
catch
{
listBox1.Items.Add("·þÎñÆ÷δÆô¶¯");
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcpClient.Connected)
{
string action = textBox1.Text.ToString();
listBox1.Items.Add("ÊäÈëµÄÐÅϢΪ:" + action);
send_stream(action);
receive_stream();
}
else
{
listBox1.Items.Add("Á¬½ÓÒѶϿª");
}
}
private void button3_Click(object sender, EventArgs e)
{
if (stream != null)
{
stream.Close();
tcpClient.Close();
socket_send.Close();
}
listBox1.Items.Add("Í˳öÓÎÏ·");
}
private void button4_Click(object sender, EventArgs e)
{
player.Load();
player.Play();
}
}
}
|