TCPClient和TCPServer进行通讯的问题

vick 2011-03-11 09:45:30
TCPClient和TCPServer进行通讯

程序开启时自动开一个线程开始监听,用的TCPServer

TCPClient发送数据给TCPServer,TCPServer先接收,然后处理,然后直接回复一个结果数据
TCPClient发送完之后立即变成接收状态,接收TCPServer的回复信息

发送程序:
tcpClient.Connect(IP, Port);
networkStream = tcpClient.GetStream();
if (networkStream.CanWrite)
{
byte[] buf = API.GetByteFromString(Context);
if (buf != null && buf.Length != 0)
{
networkStream.Write(buf, 0, buf.Length);
}
buf = new byte[1024];
networkStream.ReadTimeout = 10000;
int lens = 0;
networkStream.Read(buf, 0, buf.Length);


服务程序:
while (!bTerminated)
{
TcpClient client = server.AcceptTcpClient();
//Console.WriteLine("Connected!");

data = null;


// 获取一个数据流对象来进行读取和写入
NetworkStream stream = client.GetStream();
int i;

// 读取报文头35字节
API.ReadBytes(ref data, stream, 35);

//发送回复
string tmp = "1234";
stream.Write(Encoding.Default.GetBytes(tmp),0,4);


这样结果是,networkStream.Read(buf, 0, buf.Length);收不到东西,不设置ReadTimeout那就一直卡死,设置了收不到就到了异常处,是何原因,请指教!

请指点如何进行这种应用,这应该是很普通的应用,发个数据等服务端处理了返回处理结果,在C++里我都是直接发了就开始收等收够,在C#里为啥不行,还是因为这个自带的TCPServer/TCPClient的原因?
...全文
966 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yalan 2011-03-11
  • 打赏
  • 举报
回复

//
/* Server Program */

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv {
public static void Main() {
try {
IPAddress ipAd = IPAddress.Parse("172.21.5.99");
// use local m/c IP address, and
// use the same in the client

/* Initializes the Listener */
TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */
myList.Start();

Console.WriteLine("The server is running at port 8001...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint );
Console.WriteLine("Waiting for a connection.....");

Socket s=myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

byte[] b=new byte[100];
int k=s.Receive(b);
Console.WriteLine("Recieved...");
for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(b[i]));

ASCIIEncoding asen=new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();

}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}

---------------------------------------------------------------------------

/* Client Program */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;


public class clnt {

public static void Main() {

try {
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");

tcpclnt.Connect("172.21.5.99",8001);
// use the ipaddress as in the server program

Console.WriteLine("Connected");
Console.Write("Enter the string to be transmitted : ");

String str=Console.ReadLine();
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen= new ASCIIEncoding();
byte[] ba=asen.GetBytes(str);
Console.WriteLine("Transmitting.....");

stm.Write(ba,0,ba.Length);

byte[] bb=new byte[100];
int k=stm.Read(bb,0,100);

for (int i=0;i<k;i++)
Console.Write(Convert.ToChar(bb[i]));

tcpclnt.Close();
}

catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}

}

//

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧