服务端:
********************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace renycForm
{
public partial class Server : Form
{
public Server()
{
InitializeComponent();
}
Thread LisThread;
Socket LisSocket;
Socket newSocket;
EndPoint point;
string strmes = String.Empty;
int port = 8899;//定义侦听端口号
private void btListen_Click(object sender, EventArgs e)
{
LisThread = new Thread(new ThreadStart(BeginListern));//开线程执行BeginListern方法
LisThread.Start();//线程开始执行
}
public IPAddress GetIP()
{ /*获取本地服务器的ip地址 */
IPHostEntry iep = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ip = IPAddress.Parse("169.254.238.154");
return ip;
}
public void BeginListern()
{
LisSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Socket
IPAddress ServerIp = GetIP();/*获取本地服务器的ip地址 */
IPEndPoint iep = new IPEndPoint(ServerIp, port);
LisSocket.Bind(iep); /*将Socket绑定ip */
//toolStripStatusLabel1.Text = iep.ToString() + "正在监听";fe80::ecb5:8ecb:e765:ee9a%11
LisSocket.Listen(50); //Socket开始监听
newSocket = LisSocket.Accept();//获取连接请求的Socket
/*接收客户端Socket所发的信息 */
while (true)
{
try
{
byte[] byteMessage = new byte[1024];
newSocket.Receive(byteMessage);//接收信息
//MessageBox.Show(Encoding.Default.GetString(byteMessage));
Control.CheckForIllegalCrossThreadCalls = false;
point = newSocket.RemoteEndPoint;//获取客户端的Socket的相关信息
IPEndPoint IPpoint = (IPEndPoint)point;
strmes += IPpoint.Address.ToString() + " " + DateTime.Now.ToString() + "说:/r/n" + Encoding.Default.GetString(byteMessage).Trim(new char[] { '/0' }) + "/r/n";
this.RtRecive.Text = strmes;
}
catch (SocketException ex)
{
//toolStripStatusLabel1.Text += ex.ToString();
}
}
}
private void BtCancle_Click(object sender, EventArgs e)
{
try
{
LisSocket.Close();//关闭Socket
LisThread.Abort();//线程停止
LisThread = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Application.Exit();
}
}
private void btSend_Click(object sender, EventArgs e)
{
byte[] byteData = Encoding.Default.GetBytes(this.RtSend.Text);
newSocket.Send(byteData);//发送信息即由服务器往客户端上发信息
this.RtSend.Text = null;
}
}
}
客户端:
**********************************************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace RenSocket_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Socket ClientSocket;
private void btContent_Click(object sender, EventArgs e)
{
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string ip = "169.254.238.154";//服务器ip
IPAddress ipa = IPAddress.Parse(ip);
IPEndPoint iep = new IPEndPoint(ipa, 8899);
//this.toolStripStatusLabel1.Text = "已经建立连接.";
Control.CheckForIllegalCrossThreadCalls = false;/*如果尝试访问控件的方法或属性之一的线程不是创建该控件的线程,
* 则通常会导致不可预知的结果。
* 通常,无效的线程活动是对访问控件的 Handle 属性的错误线程的调用。
* 将 CheckForIllegalCrossThreadCalls 设置为 true 可以在调试时更容易查找并诊断此线程活动。
* 请注意在调试器外部启动应用程序时,非法跨线程调用将始终引发异常。*/
try
{
ClientSocket.Connect(iep);//连接到服务器
Thread thread = new Thread(new ThreadStart(targett));
thread.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btSend_Click(object sender, EventArgs e)
{
if (ClientSocket.Connected)//判断Socket是否已连接
{
byte[] SendMessage = new byte[100];
SendMessage = Encoding.ASCII.GetBytes(this.RtSend.Text);
ClientSocket.Send(SendMessage);//从数据中的指示位置开始将数据发送到连接的Socket。
//MessageBox.Show(Encoding.Default.GetString(SendMessage) + "发送成功!");
this.RtSend.Text = null;
}
else
{
MessageBox.Show("未建立连接!");
}
}
public void targett()
{
//this.toolStripStatusLabel1.Text = "已经建立连接准备接受数据";
while (true)
{
byte[] bytes = new byte[100];
int rev = ClientSocket.Receive(bytes, bytes.Length, 0);//将数据从连接的 Socket 接收到接收缓冲区的特定位置。
if (rev <= 0)
{
break;
}
string strev = System.Text.Encoding.Default.GetString(bytes);
this.RtRecive.AppendText("服务器对客户端说: /r/n" + strev + "/r/n");
}
}
private void btExit_Click(object sender, EventArgs e)
{
ClientSocket.Shutdown(SocketShutdown.Both);//发送完成之后停止Socket
ClientSocket.Close();//发送完成之后关闭Socket
Application.Exit();
}
}
}