基于TCP网络通信程序设计
2013-08-13 23:31
197 查看
1.服务器端
2.客户端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //添加名空间 using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace 服务器端 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool bConnected = false; private Thread tAcceptMsg = null; private IPEndPoint IPP = null;//用于Socket通信的IP地址和端口 private Socket socket = null; private Socket clientSocket = null; private NetworkStream nStream = null;//网络访问的基础数据流 private TextReader tReader = null; private TextWriter wReader = null; private void AcceptMessage() { clientSocket = socket.Accept();//一直等待连接 if (clientSocket != null) { bConnected = true; this.label1.Text = "与客户" + clientSocket.RemoteEndPoint.ToString() + "成功建立连"; } nStream = new NetworkStream(clientSocket); tReader = new StreamReader(nStream); wReader = new StreamWriter(nStream); string sTemp; while (bConnected) { try { sTemp = tReader.ReadLine(); if (sTemp.Length != 0) { lock (this) { richTextBoxReceive.Text = "客户机:" + sTemp + Environment.NewLine + richTextBoxReceive.Text; } } } catch { tAcceptMsg.Abort(); MessageBox.Show("无法与客户机进行通信。"); } } clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); socket.Shutdown(SocketShutdown.Both); socket.Close(); } private void Form1_Load(object sender, EventArgs e) { this.label1.Text = "无客户机请求连接"; } private void buttonStart_Click(object sender, EventArgs e) { //指定服务器端口,Any表示服务器侦听所有网络接口上的客户活动 IPP = new IPEndPoint(IPAddress.Any, 65535); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(IPP); socket.Listen(0);//0表示连接数量不限 //创建侦听线程 tAcceptMsg = new Thread(new ThreadStart(this.AcceptMessage)); tAcceptMsg.Start(); buttonStart.Enabled = false; } private void richTextBoxSend_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { if (bConnected) { try { lock (this) { richTextBoxReceive.Text = "我:" + richTextBoxSend.Text + richTextBoxReceive.Text; //客户机聊天信息写入网络流,以便服务器接受 wReader.WriteLine(richTextBoxSend.Text); wReader.Flush(); richTextBoxSend.Text = ""; richTextBoxSend.Focus(); } } catch { MessageBox.Show("无法与客户机通信"); } } else { MessageBox.Show("未与客户机建立连接,不能通信。"); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { socket.Close(); tAcceptMsg.Abort(); } catch{} } } }
2.客户端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //添加命名空间 using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace 客户端 { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false;//禁用此异常 } public bool bconnected = false; public Thread tAcceptMsg = null; public IPEndPoint IPP = null; public Socket socket = null; public NetworkStream nStream = null; public TextWriter wReader = null; public TextReader tReader = null; public void AcceptMessage() { string sTemp; while (bconnected) { try { sTemp = tReader.ReadLine(); if (sTemp.Length != 0) { lock (this) { richTextBoxReceive.Text = "服务器:" + sTemp + Environment.NewLine + richTextBoxReceive.Text; } } } catch { MessageBox.Show("无法与服务器通信。"); } } socket.Shutdown(SocketShutdown.Both); socket.Close(); } private void buttonLink_Click(object sender, EventArgs e) { if (textBoxIPAdress.Text == "") { MessageBox.Show("IP地址不能为空!!"); return; } if (textBoxPort.Text == "") { MessageBox.Show("端口号不能为空!!"); return; } try { IPP = new IPEndPoint(IPAddress.Parse(textBoxIPAdress.Text), int.Parse(textBoxPort.Text)); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(IPP); if (socket.Connected) { nStream = new NetworkStream(socket); tReader = new StreamReader(nStream); wReader = new StreamWriter(nStream); tAcceptMsg = new Thread(new ThreadStart(this.AcceptMessage)); tAcceptMsg.Start(); bconnected = true; buttonLink.Enabled = false; MessageBox.Show("与服务器成功建立连接,可以通信。"); } } catch { MessageBox.Show("无法与服务器通信。"); } } private void richTextBoxsend_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar ==(char)Keys.Enter) { if (bconnected) { try { lock (this) { richTextBoxReceive.Text = "我:" + richTextBoxsend.Text + richTextBoxReceive.Text; wReader.WriteLine(richTextBoxsend.Text); wReader.Flush(); richTextBoxsend.Text = ""; richTextBoxsend.Focus(); } } catch { MessageBox.Show("与服务器连接断开。"); } } else { MessageBox.Show("未与服务器建立连接,不能通信。"); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { socket.Close(); tAcceptMsg.Abort(); } catch { } } } }
相关文章推荐
- 基于TCP网络程序设计(客户机/服务器模式的通信)
- 超基础的网络编程02:基于TCP的Socket通信
- 【转】【开源下载】基于TCP网络通信的即时聊天系统(IM系统)(c#源码)
- 【开源下载】基于TCP网络通信的自动升级程序c#源码
- 基于TCP网络通信的自动升级程序源码分析-客户端请求服务器上的升级信息
- 基于linux简单的TCP/IP网络通信代码
- 基于TCP的协议封装及Netty搭建高可用网络通信服务
- Java网络编程(基于TCP和UDP的通信实现)
- 基于TCP网络通信的自动升级程序源码分析--生成升级文件相关的配置文件
- Python网络通信之socket模块(四)基于Tcp/Ip的TCP交互通信serve/client的编写过程
- 基于TCP网络通信的自动升级程序源码分析--生成升级文件相关的配置文件
- 使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
- VC6.0下select+多线程基于TCP的网络通信
- 实现采用客户/服务器通信模式,基于TCP网络通信协议的多客户端简单应用
- 基于TCP网络通信的自动升级程序源码分析-客户端请求服务器上的升级信息
- Linux网络编程之[基于socket通信的tcp协议的编程模型]
- 网络程序设计--TCP通信(服务器端)
- 基于TCP/IP的SOCKET接口实现网络通信
- socket 网络编程快速入门(二)教你编写基于UDP/TCP的服务端多线程通信