---------------------------------------------------------
using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Text
using System.Windows.Forms
using System.Net
using System.Net.Sockets
using System.Threading
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 = Int32.Parse(textBox2.Text.Substring(textBox2.Text.LastIndexOf(":") + 1))
IPAddress ip = IPAddress.Parse(textBox1.Text.Substring(textBox1.Text.LastIndexOf(":") + 1))
end = new IPEndPoint(ip, port)
}
catch
{
MessageBox.Show("输入有误!!")
textBox1.Text = "IP:"
textBox2.Text = "端口:"
return
}
try
{
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
s.Connect(end)
label1.Text = "成功联接上服务器 " + textBox1.Text + ":" + textBox2.Text
td = new Thread(new ThreadStart(bb))
td.IsBackground = true
td.Start()
}
catch
{
label1.Text = "联接失败服务器!! "
}
}
void bb()
{
while (true)
{
byte[] bb = new byte[1024]
int i= s.Receive(bb)//接收数据,返回每次接收的字节总数
string removeMsg = Encoding.Unicode.GetString(bb,0,i)
if (removeMsg == "cmd---exit")//收到的是退出通知
{
richTextBox1.Text = ""
label1.Text = "无连接"
DialogResult re=MessageBox.Show("服务器已经关闭.\n\"确定\"后退出程序,\n\"取消\"继续停留!", "消息提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
MessageBox.Show(re.ToString())
if (re == DialogResult.OK)
{
sendExit()//告诉服务器我退出了
Application.Exit()
}
return
}
richTextBox1.AppendText(removeMsg)
richTextBox1.ScrollToCaret()
}
}
private void button2_Click(object sender, EventArgs e)
{
string msg = "客户端说:" + richTextBox2.Text+"\n"
richTextBox1.AppendText(msg)
byte[] by = Encoding.Unicode.GetBytes(msg)
s.Send(by)
richTextBox2.Text = ""
richTextBox2.Focus()
richTextBox1.ScrollToCaret()
}
void sendExit()
{
string msg = "cmd---exit"
byte[] by = Encoding.Unicode.GetBytes(msg)
s.Send(by)
}
}
}
socket_server
-----------------------------------------------------------------
using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Text
using System.Windows.Forms
using System.Net
using System.Net.Sockets
using System.Threading
namespace socket_server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "监听端口" + Dns.GetHostByName(Dns.GetHostName()).AddressList[0] + ":" + Int32.Parse(textBox1.Text.Substring(textBox1.Text.LastIndexOf(":") + 1)) + "中......"
Thread td = new Thread(new ThreadStart(aa))
td.Start()
if (button1.Text == "开始监听")
{
button1.Text = "停止监听"
return
}
else
{
sendExit()
ss.Shutdown(SocketShutdown.Both)
s.Close()
td.Abort()
label1.Text = "停止监听!"
richTextBox1.Text = ""
button1.Text = "开始监听"
}
}
void sendExit()
{
string msg = "cmd---exit"
byte[] by = Encoding.Unicode.GetBytes(msg)
ss.Send(by)
}
Socket s
Socket ss
void aa()
{
int port = Int32.Parse(textBox1.Text.Substring(textBox1.Text.LastIndexOf(":") + 1))
IPEndPoint end = new IPEndPoint(Dns.GetHostByName(Dns.GetHostName()).AddressList[0], port)
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
s.Bind(end)
s.Listen(5)
ss=s.Accept()
if (ss.Connected)
{
label1.Text = "有客户端联接上端口:"+textBox1.Text
while (true)
{
byte[] by = new byte[1024]
int i = ss.Receive(by)
string msg = Encoding.Unicode.GetString(by, 0, i)
if (msg == "cmd---exit")
{
label1.Text = "客户端已经取消连接!"
return
}
richTextBox1.AppendText(msg)
richTextBox1.ScrollToCaret()
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string m = "服务器说:" + richTextBox2.Text + "\n"
richTextBox1.AppendText(m)
byte[] by = Encoding.Unicode.GetBytes(m)
ss.Send(by)
richTextBox2.Text = ""
richTextBox2.Focus()
richTextBox1.ScrollToCaret()
}
private void button3_Click(object sender, EventArgs e)
{
string w="123455"
MessageBox.Show(w.Substring(0))
}
}
}
您好,根据您的描述,可能是IP地址或DNS地址获取有问题。您需要确认一下电脑无线网卡能否正常获取到IP地址,或者您的无线网卡被设置了静态地址。* 检查IP的方法,依次点击:开始--控制面板--网络和共享中心--无线网络连接--详细信息--ipv4地址,或者点击右下角无线网络连接图标,选择CMCC点击右键,选择状态查看ipv4地址。
* IP地址范围:正常地址段:183.*.*.*;117.*.*.*;错误地址段:169.*.*.*;192.*.*.*;10.*.*.*,等。
* 如果IP地址或DNS地址有问题,请选择“自动获得IP地址”和“自动获得DNS地址”。之后重新连接就应该可以恢复了。
请您了解。
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)