---------------------------------------------------------
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemNet;
using SystemNetSockets;
using SystemThreading;
namespace socket_client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
IPEndPoint end;
Socket s;
Thread td;
private void button1_Click(object sender, EventArgs e)
{
try
{
int port = Int32Parse(textBox2TextSubstring(textBox2TextLastIndexOf(":") + 1));
IPAddress ip = IPAddressParse(textBox1TextSubstring(textBox1TextLastIndexOf(":") + 1));
end = new IPEndPoint(ip, port);
}
catch
{
MessageBoxShow("输入有误!!");
textBox1Text = "IP:";
textBox2Text = "端口:";
return;
}
try
{
s = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
sConnect(end);
label1Text = "成功联接上服务器 " + textBox1Text + ":" + textBox2Text;
td = new Thread(new ThreadStart(bb));
tdIsBackground = true;
tdStart();
}
catch
{
label1Text = "联接失败服务器!! ";
}
}
void bb()
{
while (true)
{
byte[] bb = new byte[1024];
int i= sReceive(bb); //接收数据,返回每次接收的字节总数
string removeMsg = EncodingUnicodeGetString(bb,0,i);
if (removeMsg == "cmd---exit")//收到的是退出通知
{
richTextBox1Text = "";
label1Text = "无连接";
DialogResult re=MessageBoxShow("服务器已经关闭\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtonsOKCancel, MessageBoxIconWarning);
MessageBoxShow(reToString());
if (re == DialogResultOK)
{
sendExit();//告诉服务器我退出了
ApplicationExit();
}
return;
}
richTextBox1AppendText(removeMsg) ;
richTextBox1ScrollToCaret();
}
}
private void button2_Click(object sender, EventArgs e)
{
string msg = "客户端说:" + richTextBox2Text+"\n";
richTextBox1AppendText(msg);
byte[] by = EncodingUnicodeGetBytes(msg);
sSend(by);
richTextBox2Text = "";
richTextBox2Focus();
richTextBox1ScrollToCaret();
}
void sendExit()
{
string msg = "cmd---exit";
byte[] by = EncodingUnicodeGetBytes(msg);
sSend(by);
}
}
}
socket_server
-----------------------------------------------------------------
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemNet;
using SystemNetSockets;
using SystemThreading;
namespace socket_server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1Text = "监听端口" + DnsGetHostByName(DnsGetHostName())AddressList[0] + ":" + Int32Parse(textBox1TextSubstring(textBox1TextLastIndexOf(":") + 1)) + "中";
Thread td = new Thread(new ThreadStart(aa));
tdStart();
if (button1Text == "开始监听")
{
button1Text = "停止监听";
return;
}
else
{
sendExit();
ssShutdown(SocketShutdownBoth);
sClose();
tdAbort();
label1Text = "停止监听!";
richTextBox1Text = "";
button1Text = "开始监听";
}
}
void sendExit()
{
string msg = "cmd---exit";
byte[] by = EncodingUnicodeGetBytes(msg);
ssSend(by);
}
Socket s;
Socket ss;
void aa()
{
int port = Int32Parse(textBox1TextSubstring(textBox1TextLastIndexOf(":") + 1));
IPEndPoint end = new IPEndPoint(DnsGetHostByName(DnsGetHostName())AddressList[0], port);
s = new Socket(AddressFamilyInterNetwork, SocketTypeStream, ProtocolTypeTcp);
sBind(end);
sListen(5);
ss=sAccept();
if (ssConnected)
{
label1Text = "有客户端联接上端口:"+textBox1Text;
while (true)
{
byte[] by = new byte[1024];
int i = ssReceive(by);
string msg = EncodingUnicodeGetString(by, 0, i);
if (msg == "cmd---exit")
{
label1Text = "客户端已经取消连接!";
return;
}
richTextBox1AppendText(msg);
richTextBox1ScrollToCaret();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string m = "服务器说:" + richTextBox2Text + "\n";
richTextBox1AppendText(m);
byte[] by = EncodingUnicodeGetBytes(m);
ssSend(by);
richTextBox2Text = "";
richTextBox2Focus();
richTextBox1ScrollToCaret();
}
private void button3_Click(object sender, EventArgs e)
{
string w="123455";
MessageBoxShow(wSubstring(0));
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)