我想在局域网内将服务端和客户端连接起来 但是一直连接不上 报出由于目标计算机积极拒绝无法连接
服务器端using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;
using
using System.Collections;
namespace 即时通讯1_1
{
public partial class Form1 : Form
{
private ThreadStart start;
private Thread listenThread, client_th;
static private bool m_bListening = false;
static private MyIP = ("127.0.0.1");
static private TcpListener listener = new TcpListener(MyIP, 8086);
private String msg;
ArrayList clientArray = new ArrayList();
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
start = new ThreadStart(startListen);
listenThread = new Thread(start);
}
private void btnStart_Click(object sender, EventArgs e)
{
if (m_bListening)
{
m_bListening = false;
label4.Text = "Server Idle...";
}
else
{
m_bListening = true;
listenThread.Start();
label4.Text = "Server Listening...";
}
}
private void showmore()
{
richTextBox1.Text = msg;
}
private void startListen()
{
//textBox1.Text = "listening...";
listener.Start();
//接收数据
while (m_bListening)
{
//测试是否有数据
try
{
TcpClient client = listener.AcceptTcpClient();
clientArray.Add(client);
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(AcceptMsg);
client_th = new Thread(threadStart);
client_th.Start(client);
}
catch (Exception re)
{
MessageBox.Show(re.Message);
}
}
listener.Stop(); //
}
private void AcceptMsg(object arg)
{
TcpClient client = (TcpClient)arg;
NetworkStream ns = client.GetStream();
//StreamReader sr = new StreamReader(ns);//流读写器
//字组处理
while (true)
{
try
{
byte[] bytes = new byte[1024];
int bytesread = ns.Read(bytes, 0, bytes.Length);
msg = Encoding.Default.GetString(bytes, 0, bytesread);
//显示
//MessageBox.Show(msg);
showmore();
ns.Flush();
//ns.Close();
}
catch
{
MessageBox.Show("与客户端断开连接了");
break;
}
}
}
private void btnSend_Click(object sender, EventArgs e)
{
int i=0;
foreach (TcpClient sd in clientArray) ;
object a = clientArray[i];
TcpClient client = (TcpClient)clientArray[i];
if (client == null)
return;
NetworkStream sendStream = client.GetStream();
String msg = richTextBox2.Text;
Byte[] sendBytes = Encoding.Default.GetBytes(msg);
sendStream.Write(sendBytes, 0, sendBytes.Length);
sendStream.Flush();
sendStream.Close();
i++;
}
}
}
客户端
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
using
using System.Threading;
namespace 即时通讯1_2
{
public partial class Form1 : Form
{
private TcpClient client;
private Thread client_th;
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
String mystr = richTextBox1.Text;
string ip = "127.0.0.1";
this.connect_s(ip);
}
public void connect_s(string ip)
{
try
{
client = new TcpClient(ip, 8086);
ThreadStart threadStart = new ThreadStart(AcceptMsg);
client_th = new Thread(threadStart);
client_th.Start();
label1.Text = "连接成功";
}
catch (System.Exception e)
{
label1.Text = e.ToString();
}
}
private void AcceptMsg()
{
NetworkStream ns = client.GetStream();
//StreamReader sr = new StreamReader(ns);//流读写器
//字组处理
while (true)
{
try
{
byte[] bytes = new byte[1024];
int bytesread = ns.Read(bytes, 0, bytes.Length);
string msg = Encoding.Default.GetString(bytes, 0, bytesread);
//显示
//MessageBox.Show(msg);
richTextBox1.Text = msg;
ns.Flush();
//ns.Close();
}
catch
{
MessageBox.Show("与服务器断开连接了");
break;
}
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (client == null)
return;
NetworkStream sendStream = client.GetStream();
String msg = richTextBox2.Text;
Byte[] sendBytes = Encoding.Default.GetBytes(msg);
sendStream.Write(sendBytes, 0, sendBytes.Length);
sendStream.Flush();
//sendStream.Close();
richTextBox2.Text = "";
}
}
}