while(listenerRun)//开始监听
{
Socket s = tcpl.AcceptSocket();
string remote = s.RemoteEndPoint.ToString();
Byte[] stream = new Byte[20000]; //sring's length can not more than 80
int i=s.Receive(stream);//接受连接请求的字节流
string msg =System.Text.Encoding.UTF8.GetString(stream);
Console.WriteLine(msg);//在控制台显示字符串
} 我这个msg很长的时候,好像传值会丢失,不知道怎么改啊,需要传很长的字符串请赐教,郁闷了一天了
{
Socket s = tcpl.AcceptSocket();
string remote = s.RemoteEndPoint.ToString();
Byte[] stream = new Byte[20000]; //sring's length can not more than 80
int i=s.Receive(stream);//接受连接请求的字节流
string msg =System.Text.Encoding.UTF8.GetString(stream);
Console.WriteLine(msg);//在控制台显示字符串
} 我这个msg很长的时候,好像传值会丢失,不知道怎么改啊,需要传很长的字符串请赐教,郁闷了一天了
你在要传输的信息里加上个结束标志,如:<the end>
byte []buffer=new byte[1024];
我这里采用的是异步套接字:
public void ReceiveCallback(IAsyncResult ar)
{
handler=(Socket)ar.AsyncState;
int bytesread=handler.EndReceive(ar);
//if there is some data...
if(bytesread>0)
{
box=System.Text.Encoding.Default.GetString(buffer,0,buffer.Length);
box=box.Trim();
//see the file is over
if(box.IndexOf("<the end>")>0)
{
string []a=box.Split('&');
string b=a[1].Replace("<the end>",""); byte []bytedata=System.Text.Encoding.Default.GetBytes("文件已接受结束");
//send the data
handler.BeginSend(bytedata,0,bytedata.Length,0,new AsyncCallback(this.sendcallback),handler);
}
else
{//如果没有结束,继续接受
handler.BeginReceive(buffer,0,buffer.Length,0,new AsyncCallback(ReceiveCallback),handler);
}
}
}
}