基于TCP网络通信程序设计
来源:互联网 发布:投影仪有网络连接 编辑:程序博客网 时间:2024/05/02 04:30
1.服务器端
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网络通信程序设计
- TCP 基于连接的Socket通信程序设计
- 基于TCP的网络通信(SCOKET)
- 基于TCP协议的网络通信
- QT-基于TCP的网络通信
- Socket网络编程:基于TCP多线程通信
- java 基于TCP的Socket网络通信
- Android 基于TCP协议的网络通信
- Linux与网络通信:基于TCP的网络聊天室
- winsock网络编程——基于TCP的程序设计和基于UDP的程序设计
- 基于TCP的网络通信程序演示[命令行程序]
- 基于TCP/IP的SOCKET接口实现网络通信
- VC6.0下select+多线程基于TCP的网络通信
- WSAEventSelect模型基于TCP协议的控制台网络通信程序
- 基于linux简单的TCP/IP网络通信代码
- 基于TCP网络通信的简易多线程GUI聊天室
- 安卓学习_基于TCP协议的网络通信
- 基于TCP协议的网络通信(socket编程)
- JavaScript中childNodes、children、nodeValue、nodeType、parentNode、nextSibling详细讲解
- 一个简单的字符程序
- socket字节流解析(网络抓包解析)
- 校招季——编程题目(3-5)
- Nutch1.2+Tomcat7.0+Cygwin网页爬虫工具搭建
- 基于TCP网络通信程序设计
- Linux操作系统--1Linux的档案权限与目录配置(1)
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解
- memcache及其telnet命令使用详解
- 虚拟列表2
- eclipse导入的Android项目没有android.jar包并报错
- 线程开发禁忌
- zoj 3165(二分图最大点权独立)
- he type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files