socket 问题,如果通过byte方式传数据,如果,byte数组超过1024个字节就会被截断,如果解决,在线等!!!

xuxf 2005-06-23 07:28:39
rt
...全文
963 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuxf 2005-06-24
  • 打赏
  • 举报
回复
没有高手阿?
xuxf 2005-06-24
  • 打赏
  • 举报
回复
我试过一次一次传1024个字节的。但是还是失败。就是到了2048个字节的。
我还试了一下。windows之间好象没有这个问题的。
upshania 2005-06-24
  • 打赏
  • 举报
回复
不要一口气传。。。。分成好几个小块来做
xuxf 2005-06-24
  • 打赏
  • 举报
回复
unix上的程序接收到了数据。但是从2048个字节后面的内容都是0。
Raxxxer 2005-06-24
  • 打赏
  • 举报
回复
自己提前一下。to yangjundeng(天下无双) .一定要是同步做的呀。
yangjundeng 2005-06-23
  • 打赏
  • 举报
回复
http://dev.csdn.net/develop/article/65/65798.shtm使用这个Socket写好的,不会出现这个问题。
codeangel 2005-06-23
  • 打赏
  • 举报
回复
关注,我也在做这方法的东西
Raxxxer 2005-06-23
  • 打赏
  • 举报
回复
networkStream.Write(SendBuffer, 0, 8000);
Raxxxer 2005-06-23
  • 打赏
  • 举报
回复
xuxf的同事。
现在把buffer加到8000。
但是和unix通讯的时候,unix只收到2048个字节。
unix上的程序没有问题。
adow 2005-06-23
  • 打赏
  • 举报
回复
我不知道是不是真有这种问题,如果是的话,那就把数据拆分了再传输,比如每次100个字节。
gdami 2005-06-23
  • 打赏
  • 举报
回复
Byte[] SendBuffer = new Byte[1000];
networkStream.Write(SendBuffer, 0, 1000);

-_-!

1000<1024,不会是这个问题吧。
xuxf 2005-06-23
  • 打赏
  • 举报
回复
public class WorkFlow{
public WorkFlow(Socket socket){
_socket = socket;
dataMDL = new Datamdl();
}

public void ThreadProc(){
int records, offset;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

Byte[] ReceiveBuffer = new Byte[256];
Byte[] SendBuffer = new Byte[1000];

DataTable dataTable;

NetworkStream networkStream = new NetworkStream(_socket);
networkStream.Read(ReceiveBuffer, 0, 256);

string receive = new string(encoding.GetChars(ReceiveBuffer));
Console.WriteLine("Received from client of inquire: [" + receive + "]");

string OperationType = receive.Substring(0, 3);
-----------------------------------
networkStream.Write(SendBuffer, 0, 1000);
//networkStream.Flush();
networkStream.Close();
_socket.Close();
//System.Threading.Thread.CurrentThread.Abort();
Console.WriteLine("Now End a conversation.****");
return;
}


private void String2ByteArray(ref Byte[] ArrayByte, int start, string convert, int length){
int len = ArrayByte.Length;
if(start+length>len) return;
if(convert.Length<length) length=convert.Length;

Console.WriteLine("=Start to Convert=" + convert);
Byte[] tmpByteArray = System.Text.Encoding.Default.GetBytes(convert);
tmpByteArray.CopyTo(ArrayByte, start);
//Console.WriteLine("=End to Convert="+ System.Text.ASCIIEncoding.ASCII.GetChars(tmpByteArray));
}

private Socket _socket;
private Datamdl dataMDL;
}
gdami 2005-06-23
  • 打赏
  • 举报
回复
网络问题吧。 把超时设定长一点试试.

s1为Socket实例。
s1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 30000); //30秒
s1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30000);
xuxf 2005-06-23
  • 打赏
  • 举报
回复
public void Run(){
System.Threading.ThreadStart _delegate = null;
System.Threading.Thread _thread = null;
running = MakeSocket();

int loopTime = 0;

while(running){
Console.WriteLine("*Start to listen a new connection. "+ (++loopTime).ToString());

try{
client = listener.AcceptSocket();
}catch(Exception e){
Console.WriteLine(e.Message);
break;
}
try{
WorkFlow workFlow = new WorkFlow(client);

_delegate = new System.Threading.ThreadStart(workFlow.ThreadProc);
_thread= new System.Threading.Thread(_delegate);
_thread.Start();
}catch(System.Threading.ThreadAbortException e){
Console.WriteLine(e.Message);
listener.Stop();
return;
}catch(Exception e){
Console.WriteLine(e.Message);
break;
}
}
listener.Stop();
}
dazhu2 2005-06-23
  • 打赏
  • 举报
回复
没有这种问题,但如果每次发送字节数太大好像容易掉包,你把你代码贴出来看看
  • 打赏
  • 举报
回复
没有这种问题,把你的代码贴上来看看

110,571

社区成员

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

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

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