我有一个客户端服务器应用程序使用对象进行通信
当我只从客户端向服务器发送一个对象时,一切正常.
当我尝试在同一个流上一个接一个地发送几个对象时,我得到了
StreamCorruptedException.
Run Code Online (Sandbox Code Playgroud)
有人可以指导我这个错误的原因吗?
客户端写方法
private SecMessage[] send(SecMessage[] msgs)
{
SecMessage result[]=new SecMessage[msgs.length];
Socket s=null;
ObjectOutputStream objOut =null;
ObjectInputStream objIn=null;
try
{
s=new Socket("localhost",12345);
objOut=new ObjectOutputStream( s.getOutputStream());
for (SecMessage msg : msgs)
{
objOut.writeObject(msg);
}
objOut.flush();
objIn=new ObjectInputStream(s.getInputStream());
for (int i=0;i<result.length;i++)
result[i]=(SecMessage)objIn.readObject();
}
catch(java.io.IOException e)
{
alert(IO_ERROR_MSG+"\n"+e.getMessage());
}
catch (ClassNotFoundException e)
{
alert(INTERNAL_ERROR+"\n"+e.getMessage());
}
finally
{
try {objIn.close();} catch (IOException e) {}
try {objOut.close();} catch (IOException e) {}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
服务器读取方法
//in is an inputStream Defined in …Run Code Online (Sandbox Code Playgroud) 我试图创建一个动态比例的矩阵,并在这里初始化它是我用来分配内存和初始化的代码:
int **matrix;
//mem allocation
matrix=(int*)malloc(sizeof(int*)*mat_w);
for (i=0;i<mat_w;i++)
matrix[i]=(int)malloc(sizeof(int)*mat_h);
//init
for (i=0;i<mat_w;i++)
for (j=0;j<mat_h;j++)
matrix[i][j]=0;
Run Code Online (Sandbox Code Playgroud)
这个,工作得很好,问题是,如果我尝试创建一个short类型的矩阵 - 我在init第一次传递时得到分段错误.
这是C语言问题还是我做错了什么?
矩阵代码类型short:
short **matrix;
//mem allocation
matrix=(short*)malloc(sizeof(short*)*mat_w);
for (i=0;i<mat_w;i++)
matrix[i]=(short)malloc(sizeof(short)*mat_h);
//init
for (i=0;i<mat_w;i++)
for (j=0;j<mat_h;j++)
matrix[i][j]=0;
Run Code Online (Sandbox Code Playgroud)
PS:为了清晰的代码,我放弃了安全检查,索引变量和边界声明.
谢谢,
亚历克斯