服务器端程序: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 ServerTest
{
public partial class ServerT : Form
{ private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
private int IPPort = 10001;
private IPEndPoint myServer;
private Socket mySocket;
private Socket Test;
private String Msg; public ServerT()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
formDo();
} private void formDo() {
try
{
Thread vThread = new Thread(new ThreadStart(acc));
vThread.Start();
}
catch (Exception ee)
{
rTB1.AppendText("Error: " + ee.Message + "\r\n");
}
}
private void invokeRTB1()
{
rTB1.AppendText(Msg);
} private void acc()
{
myServer = new IPEndPoint(vAddr, IPPort);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(myServer);
mySocket.Listen(5);
Msg = "Wait Connection ...\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
int vConnected = 0;
while (true)
{
Test = mySocket.Accept();
if (Test.Connected)
{
if (vConnected == 0)
{
vConnected = 1;
sendM("Connection Successfully\r\n");
}
Msg = "Connection Successfully\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
Thread vThread = new Thread(new ThreadStart(connectionNow));
vThread.Start();
}
}
} private void connectionNow()
{
while (true)
{
Byte[] testReadStr = new byte[64];
NetworkStream readStr = new NetworkStream(Test);
readStr.Write(testReadStr, 0, testReadStr.Length);
Msg = Encoding.Unicode.GetString(testReadStr);
if (Msg.Length != 0)
{
Msg = Msg + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
}
} private void button1_Click(object sender, EventArgs e)
{
sendM(tB1.Text+"\r\n");
rTB1.AppendText(tB1.Text + "\r\n");
} private void sendM(String Message)
{
NetworkStream sendStr = new NetworkStream(Test);
Byte[] testSendStr = new byte[64];
String sendMessage = Message;
//testSendStr = UnicodeEncoding.Unicode.GetBytes(sendMessage);
testSendStr = Encoding.Unicode.GetBytes(sendMessage);
sendStr.Write(testSendStr, 0, testSendStr.Length);
}
}
public delegate void RichTextBoxStreamType();
}客户端程序: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 ClientTest
{
public partial class Form1 : Form
{
private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
private int IPPort = 10001;
private IPEndPoint myServer;
private Socket mySocket;
private String Msg; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
formDo();
} private void Butt1_Click(object sender, EventArgs e)
{ }
private void invokeRTB1()
{
rTB1.AppendText(Msg);
} private void formDo()
{
try
{
Thread vThread = new Thread(new ThreadStart(acc));
vThread.Start();
}
catch (Exception ee)
{
rTB1.AppendText("Error: " + ee.Message + "\r\n");
}
} private void acc()
{
try
{
myServer = new IPEndPoint(vAddr, IPPort);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Connect(myServer);
Thread vThread = new Thread(new ThreadStart(connectionNow));
vThread.Start();
Msg = "连接成功\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
catch (Exception ee)
{
Msg = ee.Message + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
} private void connectionNow()
{
while (true)
{
try
{
Byte[] testReadStr = new byte[64];
NetworkStream readStr = new NetworkStream(mySocket);
readStr.Write(testReadStr, 0, testReadStr.Length);
Msg = Encoding.Unicode.GetString(testReadStr);
if (Msg.Length != 0)
{
Msg = Msg + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
}
catch (Exception ee)
{
mySocket.Close();
Msg = ee.Message + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
break;
}
}
} public delegate void RichTextBoxStreamType();
}
}服务器端有三个控件,一个rTB1是RichTextBox,用来记录显示发送和接收的信息,一个tB1是用来发送信息的,还有一个就是按钮。
客户端也有三个控件,功能和服务器端的一样。客户端发送信息的部分还没写,仅仅只是连接上服务器,然后接收服务器端的信息着关就过不了。连接上后,服务器端在tB1中输入一串字符,按按钮发送后,程序就死了。字符串也没有传送到客户端。我如果不用自己写的客户端连,而是用Command窗口中telnet命令连接上后,服务器端却可以正常发送字符串。
请问这是怎么回事啊?是不是客户端接收字符串的代码出错了?
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 ServerTest
{
public partial class ServerT : Form
{ private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
private int IPPort = 10001;
private IPEndPoint myServer;
private Socket mySocket;
private Socket Test;
private String Msg; public ServerT()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
formDo();
} private void formDo() {
try
{
Thread vThread = new Thread(new ThreadStart(acc));
vThread.Start();
}
catch (Exception ee)
{
rTB1.AppendText("Error: " + ee.Message + "\r\n");
}
}
private void invokeRTB1()
{
rTB1.AppendText(Msg);
} private void acc()
{
myServer = new IPEndPoint(vAddr, IPPort);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Bind(myServer);
mySocket.Listen(5);
Msg = "Wait Connection ...\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
int vConnected = 0;
while (true)
{
Test = mySocket.Accept();
if (Test.Connected)
{
if (vConnected == 0)
{
vConnected = 1;
sendM("Connection Successfully\r\n");
}
Msg = "Connection Successfully\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
Thread vThread = new Thread(new ThreadStart(connectionNow));
vThread.Start();
}
}
} private void connectionNow()
{
while (true)
{
Byte[] testReadStr = new byte[64];
NetworkStream readStr = new NetworkStream(Test);
readStr.Write(testReadStr, 0, testReadStr.Length);
Msg = Encoding.Unicode.GetString(testReadStr);
if (Msg.Length != 0)
{
Msg = Msg + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
}
} private void button1_Click(object sender, EventArgs e)
{
sendM(tB1.Text+"\r\n");
rTB1.AppendText(tB1.Text + "\r\n");
} private void sendM(String Message)
{
NetworkStream sendStr = new NetworkStream(Test);
Byte[] testSendStr = new byte[64];
String sendMessage = Message;
//testSendStr = UnicodeEncoding.Unicode.GetBytes(sendMessage);
testSendStr = Encoding.Unicode.GetBytes(sendMessage);
sendStr.Write(testSendStr, 0, testSendStr.Length);
}
}
public delegate void RichTextBoxStreamType();
}客户端程序: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 ClientTest
{
public partial class Form1 : Form
{
private IPAddress vAddr = IPAddress.Parse("192.168.1.100");
private int IPPort = 10001;
private IPEndPoint myServer;
private Socket mySocket;
private String Msg; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
formDo();
} private void Butt1_Click(object sender, EventArgs e)
{ }
private void invokeRTB1()
{
rTB1.AppendText(Msg);
} private void formDo()
{
try
{
Thread vThread = new Thread(new ThreadStart(acc));
vThread.Start();
}
catch (Exception ee)
{
rTB1.AppendText("Error: " + ee.Message + "\r\n");
}
} private void acc()
{
try
{
myServer = new IPEndPoint(vAddr, IPPort);
mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
mySocket.Connect(myServer);
Thread vThread = new Thread(new ThreadStart(connectionNow));
vThread.Start();
Msg = "连接成功\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
catch (Exception ee)
{
Msg = ee.Message + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
} private void connectionNow()
{
while (true)
{
try
{
Byte[] testReadStr = new byte[64];
NetworkStream readStr = new NetworkStream(mySocket);
readStr.Write(testReadStr, 0, testReadStr.Length);
Msg = Encoding.Unicode.GetString(testReadStr);
if (Msg.Length != 0)
{
Msg = Msg + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
}
catch (Exception ee)
{
mySocket.Close();
Msg = ee.Message + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
break;
}
}
} public delegate void RichTextBoxStreamType();
}
}服务器端有三个控件,一个rTB1是RichTextBox,用来记录显示发送和接收的信息,一个tB1是用来发送信息的,还有一个就是按钮。
客户端也有三个控件,功能和服务器端的一样。客户端发送信息的部分还没写,仅仅只是连接上服务器,然后接收服务器端的信息着关就过不了。连接上后,服务器端在tB1中输入一串字符,按按钮发送后,程序就死了。字符串也没有传送到客户端。我如果不用自己写的客户端连,而是用Command窗口中telnet命令连接上后,服务器端却可以正常发送字符串。
请问这是怎么回事啊?是不是客户端接收字符串的代码出错了?
while (true)
{
Test = mySocket.Accept();
if (Test.Connected)
{
if (vConnected == 0)
{
vConnected = 1;
sendM("Connection Successfully\r\n");
}
Msg = "Connection Successfully\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
Thread vThread = new Thread(new ThreadStart(connectionNow)); //一旦接收连接成功,即Test.Connected =1 以后,这里永远都在做...
vThread.Start();
}
}
http://www.mysticboy.cn/article.asp?id=7
http://www.mysticboy.cn/article.asp?id=9
private void connectionNow()
{
while (true)
{
try
{
Byte[] testReadStr = new byte[64];
NetworkStream readStr = new NetworkStream(mySocket);
readStr.Write(testReadStr, 0, testReadStr.Length);//?????Read or Write
Msg = Encoding.Unicode.GetString(testReadStr);
if (Msg.Length != 0)
{
Msg = Msg + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
}
}
catch (Exception ee)
{
mySocket.Close();
Msg = ee.Message + "\r\n";
rTB1.BeginInvoke(new RichTextBoxStreamType(invokeRTB1));
break;
}
}
}
一语惊醒梦中人,都是Copy惹的祸