java Socket接收資料亂碼問題

毛毛蟲發表於2006-08-02
系統傳送資料程式碼:
socket = new Socket(SysConfigInfo.getConfigInfo().getMsgIp(),
SysConfigInfo.getConfigInfo().getMsgPort());
outStream = new DataOutputStream(socket.getOutputStream());
//優先順序
outStream.writeInt(nOrder);
//動作
outStream.writeInt(nAction);
//型別
outStream.writeInt(nMode);
outStream.flush();
outStream.close();
outStream = null;
socket.close();
接收socket的資料用C寫的,現在需要用java寫,如下:
public ServerThread(Socket s)
{
//接收receiveServer傳來的套接字
this.m_ClientRequest = s;

InputStreamReader reader;
OutputStreamWriter writer;

try
{
//初始化輸入、輸出流
reader = new InputStreamReader(m_ClientRequest.getInputStream());
writer = new OutputStreamWriter(m_ClientRequest.getOutputStream());
//out = new PrintWriter(m_ClientRequest.getOutputStream(),true);

m_Input = new BufferedReader(reader);
m_Output = new PrintWriter(writer,true);

}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}

public void run()
{
String strSql = null;
char [] buffer = new char[8192];
String sReturn = null;

int nDataLen;

Msg = new ShowMsg("資料處理執行緒啟動:" + getName());

try
{
//讀取客戶端傳送的資料
nDataLen = m_Input.read(buffer);

if(nDataLen > 0 )
{
//strSql = String.valueOf(buffer,0,nDataLen - 1 );
strSql = buffer.toString();
byte[] tmp = strSql.getBytes();


//System.out.println("" +tmp);
for(int i = 0; i < tmp.length; i ++){
//out.print(tmp);
System.out.println("" + String.valueOf(tmp));
}

相關文章