C# socket详解

C# socket详解,第1张

服务器端代码:
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemThreading;
using SystemNet;
using SystemNetSockets;
namespace ChatToolServer
{
public partial class Form1 : Form
{
//server-用于处理客户端连接请求的socket
Socket clientSocket = null;
delegate void del();

public Form1()
{
InitializeComponent();
}
//server-侦听方法
private void listen()
{
//获取服务器IP
string hostName = DnsGetHostName();
IPAddress[] ip = DnsGetHostAddresses(hostName);
IPAddress HostIp = ip[0];
//创建一个网络端点
IPEndPoint iep = new IPEndPoint(HostIp, 82);
//创建服务端服务端套接
Socket serverSocket = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
//将套接字与网络端点绑定
serverSocketBind(iep);
//将套接字置为侦听状态,并设置最大队列数为10
serverSocketListen(10);
//以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket
//新的套接字:包含对方计算机的IP和端口号,可使用这个套接字与本机进行通信
clientSocket = serverSocketAccept();
if (clientSocket != null)
{
MessageBoxShow("连接成功!");
}

}
private void send_Click(object sender, EventArgs e)
{
if (thistextBox1Text != "")//不能发送空消息
{
try
{
//发送数据
string message = textBox1Text;
byte[] sendbytes = SystemTextEncodingUTF8GetBytes(message);
int successSendBtyes = clientSocketSend(sendbytes, sendbytesLength, SocketFlagsNone);
}
catch (Exception exp)
{
MessageBoxShow(expMessage);
}
//将发送的数据显示到对话窗口并使对话窗口的滚动条一直停留在最下方
thistextBox2Text +="服务器:"+"\r\n" +textBox1Text + "\r\n";//发完一条消息就换行显示
thistextBox2SelectionStart = thistextBox2TextLength;
thistextBox2ScrollToCaret();
thistextBox1Text = "";//将发送窗口清空

}
else
{
MessageBoxShow("发送内容不能为空");
}

}
private void Form1_Load(object sender, EventArgs e)
{
//server-创建并运行侦听线程
Thread threadListen = new Thread(new ThreadStart(listen));
threadListenStart();
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] receiveBytes = new byte[1024];
//如果侦听后取得客户端连接,并且客户端的缓冲区中有内容可读,开始接收数据
if (clientSocket != null)
{
if (clientSocketPoll(100, SelectModeSelectRead))
{
int successReceiveBytes = clientSocketReceive(receiveBytes);
thistextBox2Text += "客户端:" +"("+ clientSocketRemoteEndPointToString()+")"+"\r\n" +
SystemTextEncodingUTF8GetString(receiveBytes, 0, successReceiveBytes) + "\r\n";
thistextBox2SelectionStart = thistextBox2TextLength;//使对话窗口的滚动条一直停留在最下方
thistextBox2ScrollToCaret();
}
}
}
}
}
客户端代码;
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemNet;
using SystemNetSockets;
using SystemThreading;
namespace ChatToolClient
{
public partial class Form1 : Form
{
Socket clientSocket = null;//客户端套接字
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//建立与服务器连接的套接字
IPAddress ip = IPAddressParse("1721694134");
IPEndPoint iep = new IPEndPoint(ip, 82);
clientSocket = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
clientSocketConnect(iep);
textBox2Text = "连接成功" + "\r\n";

}
catch (Exception exp)
{
MessageBoxShow(expMessage);
}
}
private void send_Click(object sender, EventArgs e)
{
if (textBox1Text != "")
{
try
{
//发送数据
string message = textBox1Text;
byte[] sendbytes = SystemTextEncodingUTF8GetBytes(message);
int successSendBtyes = clientSocketSend(sendbytes, sendbytesLength, SocketFlagsNone);
}
catch (Exception exp)
{
MessageBoxShow(expMessage);
}
//将发送的数据显示到对话窗口并使对话窗口的滚动条一直停留在最下方
thistextBox2Text += "我自己:"+"\r\n"+textBox1Text + "\r\n";//发完一条消自己息就换行显示
thistextBox2SelectionStart = thistextBox2TextLength;
thistextBox2ScrollToCaret();
thistextBox1Text = "";//将发送窗口清空
}
else
{
MessageBoxShow("发送内容不能为空");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] receiveBytes = new byte[1024];
if (clientSocketPoll(100, SelectModeSelectRead))
{
int successReceiveBytes = clientSocketReceive(receiveBytes);
thistextBox2Text +="服务器:"+"\r\n"+
SystemTextEncodingUTF8GetString(receiveBytes, 0, successReceiveBytes) + "\r\n";
thistextBox2SelectionStart = thistextBox2TextLength;//使对话窗口的滚动条一直停留在最下方
thistextBox2ScrollToCaret();
}
}
}
}
另外,本人有很多自己写的Socket代码,包括TCP,UDP并且同步,异步方法的都有阁下需要的话,可以联系本人 E-mail:pjw216@126com


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10936974.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存