图文版网络聊天程序
来源:互联网 发布:淘宝vip会员消费金额 编辑:程序博客网 时间:2024/06/15 23:36
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.Net;using System.Net.Sockets;using System.Threading;namespace 服务器端{ public partial class Form1 : Form { private Socket socket; private Socket clientSocket; private Thread tAcceptMsg; public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void AcceptMessage() { clientSocket = socket.Accept(); this.labelState.Text = "与客户机" + clientSocket.RemoteEndPoint.ToString() + "成功建立连接。"; while (true) { try { NetworkStream nStream = new NetworkStream(clientSocket); byte[] datasize = new byte[4]; nStream.Read(datasize, 0, 4); int size = System.BitConverter.ToInt32(datasize, 0); byte[] Message = new byte[size]; int dataleft = size; int start = 0; while (dataleft > 0) { int recv = nStream.Read(Message, start, dataleft); start += recv; dataleft -= recv; } this.richTextBoxReceive.Rtf = System.Text.Encoding.Unicode.GetString(Message); } catch { this.labelState.Text = "与客户机断开连接。"; break; } } } private void buttonStatr_Click(object sender, EventArgs e) { this.buttonStatr.Enabled = false; IPAddress ip = IPAddress.Parse(this.textBoxIP.Text); IPEndPoint IPP = new IPEndPoint(ip, Int32.Parse(this.textBoxPort.Text)); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Bind(IPP); socket.Listen(10); tAcceptMsg = new Thread(new ThreadStart(AcceptMessage)); tAcceptMsg.Start(); } private void richTextBoxSend_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { string str = this.richTextBoxSend.Rtf; int i = str.Length; if (i == 0) return; else i *= 2; //str为UniCode编码,每个字符占2B,实际字节数应*2 byte[] datasize = new byte[4]; //将32位整数值转换为字节数组 datasize = System.BitConverter.GetBytes(i); byte[] sendbytes = System.Text.Encoding.Unicode.GetBytes(str); try { NetworkStream netStream = new NetworkStream(clientSocket); netStream.Write(datasize, 0, 4); netStream.Write(sendbytes, 0, sendbytes.Length); netStream.Flush(); this.richTextBoxSend.Rtf = ""; } catch { MessageBox.Show("信息无法发送!"); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { socket.Close(); tAcceptMsg.Abort(); } catch { } } }}
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.Net;using System.Net.Sockets;using System.Threading;namespace 客户端{ public partial class Form1 : Form { private Socket socket; private Thread tAcceptMsg; public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void AcceptMessage() { while (true) { try { NetworkStream nstream = new NetworkStream(socket); byte[] datasize = new byte[4]; nstream.Read(datasize, 0, 4); int size = System.BitConverter.ToInt32(datasize, 0); byte[] message = new byte[size]; int dataleft = size,start=0; while (dataleft > 0) { int recv = nstream.Read(message, start, dataleft); start += recv; dataleft -= recv; } this.richTextBoxReceive.Rtf = System.Text.Encoding.Unicode.GetString(message); } catch { } } } private void richTextBoxSend_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { string str = this.richTextBoxSend.Rtf; int i = str.Length; if (i == 0) return; else i *= 2; byte[] datasize = new byte[4]; datasize = System.BitConverter.GetBytes(i); byte[] sendBytes = System.Text.Encoding.Unicode.GetBytes(str); try { NetworkStream netStream = new NetworkStream(socket); netStream.Write(datasize, 0, 4); //发送信息长度大小 netStream.Write(sendBytes, 0, sendBytes.Length);//发送信息 netStream.Flush(); this.richTextBoxSend.Rtf = ""; } catch { MessageBox.Show("信息无法发送!!"); } } } private void buttonLink_Click(object sender, EventArgs e) { IPAddress ip = IPAddress.Parse(this.textBoxIP.Text); IPEndPoint IPP = new IPEndPoint(ip, Int32.Parse(this.textBoxPort.Text)); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { socket.Connect(IPP); } catch { MessageBox.Show("与服务器连接失败!"); return; } this.labelState.Text = "与服务器" + this.textBoxIP.Text + ":" + this.textBoxPort.Text + "成功建立连接。"; tAcceptMsg = new Thread(new ThreadStart(AcceptMessage)); tAcceptMsg.IsBackground = true; tAcceptMsg.Start(); this.buttonLink.Enabled = false; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { socket.Close(); tAcceptMsg.Abort(); } catch { } } }}
- 图文版网络聊天程序
- Java网络聊天程序
- udp网络聊天控制台程序
- C# UDP 网络聊天程序
- c# 多线程网络聊天程序
- C++ builder 网络聊天程序
- 网络通信聊天程序(群聊)
- 聊天程序初级版
- 简单的网络聊天程序,MFC实现
- 简单QQ网络聊天机程序
- LINUX网络编程,简单的聊天程序
- java 聊天程序 socket网络编程
- 基于VC的网络聊天程序
- 基于多进程的网络聊天程序
- java网络编程实现一个聊天程序
- 网络编程编写一个聊天程序
- 利用C#编写的网络聊天程序
- JAVA 网络编程 UDP(聊天程序)
- JOBB
- gdb调试器自定义命令
- 面向接口编程详解(一)——思想基础
- python调试常用函数
- ubuntu一些脚本的执行顺序
- 图文版网络聊天程序
- Office 2010一键激活工具一键激活Office 2010 Professional Plus
- cocos2d-x ios移植android
- Code Fragment-确定一个类的字段顺序,并在使用的地方坚持这个顺序
- Multiple APK Support
- java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.37\webapps\host-manager does
- [每日一题] OCP1z0-047 :2013-08-13 Flashback Version Query.....................................36
- thrift之TServer类体系原理及源码详细解析之服务器基类TServer
- Android开发中几个最基本的概念