放在外网的tcp服务端怎么写( 二 )


接收到这个最终FIN的原发送端TCP(主动要求关闭连接的那一端)确认这个FIN 。
因为每个方向都需要一个FIN和ACK,所以断开需要4个次连接 。
4.C# TCP/IP中的客户端和服务器在局域网或外网怎么通信呀这是我以前在学校的时候做的一个c/s系统里面的类似qq聊天工具 你看看 。
这是客服端的代码: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.Threading;using System.Net;using System.Net.Sockets;namespace demo2{ public partial class frmTouchWe : Form { Thread a; //发送IP跟端口 private UdpClient udp = new UdpClient("127.0.0.1",9000); //接收端口 private UdpClient udpSend = new UdpClient(8000); private IPEndPoint ip = new IPEndPoint(IPAddress.Any,0); public frmTouchWe() { InitializeComponent(); Form. = false; } private void frmTouchWe_Load(object sender, EventArgs e) { //定义线程开始 a = new Thread(new ThreadStart(Run)); a.Start(); }private void linkLabel1_LinkClicked(object sender, e) { //客服接收信息 。
弹出窗体 frmAnswerInfo answerInfo = new frmAnswerInfo(); answerInfo.Show(); } private void button1_Click(object sender, EventArgs e) { //在自己的信息栏中显示自己的信息 lsbInfo.Items.Add(txtInfo.Text); //用户信息内容 string Mes = txtInfo.Text; //用户信息 string mes = "用户:"+Program.user.id+"("+Program.user.name+")"+" " + time; //转换成字节 byte[] b = UTF8Encoding.UTF8.GetBytes(Mes); byte[] bb = UTF32Encoding.UTF8.GetBytes(mes); //发送信息 udp.Send(bb, bb.Length); udp.Send(b, b.Length); txtInfo.Text = ""; } //循环接受客服发来的信息 private void Run() { while (true) { byte[] b = udpSend.Receive(ref ip); string mes = UTF8Encoding.UTF8.GetString(b); lsbInfo.Items.Add(mes); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void button3_Click_1(object sender, EventArgs e) { //清空所有项 lsbInfo.Items.Clear(); } }}下面是服务器端的代码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.Threading;using System.Net;using System.Net.Sockets;namespace demo2{ public partial class frmAnswerInfo : Form { //定义一条线程,用来循环接收客户发来的信息 Thread a; //定义另一条线程,用来升起窗体 Thread b; //发送到信息的地址 private UdpClient udpSend = new UdpClient("127.0.0.1",8000); //接收端口 private UdpClient udp = new UdpClient(9000); private IPEndPoint ip = new IPEndPoint(IPAddress.Any,0); public frmAnswerInfo() { InitializeComponent(); Form. = false; } //循环接收信息 private void Run() { while (true) { byte[] b = udp.Receive(ref ip); string mes = UTF8Encoding.UTF8.GetString(b); lsbInfo.Items.Add(mes); } } private void frmAnswerInfo_Load(object sender, EventArgs e) { //设置窗体的位置属性(窗体加载时候慢慢从右下角升上来~类似qq广告~~) this.Top = Screen.PrimaryScreen.WorkingArea.Height; this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width; //设置窗体的名字 a = new Thread(new ThreadStart(Run)); b = new Thread(new ThreadStart(run)); //开始线程 a.Start(); b.Start(); } //用户点击谈话时显示该窗体 private void run() { while (true) { this.Top = this.Top - 10; Thread.Sleep(100); if (Screen.PrimaryScreen.WorkingArea.Height - this.Height >= this.Top) { break; } } } //发送按钮编码 private void button1_Click(object sender, EventArgs e) { //获取当前时间 DateTime time = DateTime.Now; //在自己的信息栏中显示自己发出去的信息 lsbInfo.Items.Add("在线客服:"+time); lsbInfo.Items.Add(txtInfo.Text); //在客户端显示自己的信息标题 string mes = "在线客服:"+time; //信息内容 string Mes = txtInfo.Text; //发送信息标题 byte[] b = UTF8Encoding.UTF8.GetBytes(mes); //发送信息内容 byte[] bb = UTF8Encoding.UTF8.GetBytes(Mes); udpSend.Send(b, b.Length); udpSend.Send(bb,bb.Length); txtInfo.Text = ""; } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void button3_Click(object sender, EventArgs e) { //清除所有项 lsbInfo.Items.Clear(); } }}这个可以跑的 。