서버와 클라이언트가 소켓통신하려는데요 데이타가 꽤 큽니다. MB 단위요
그래서 보내주는 데이타를 받아올때는 아래의 함수를 이용하였습니다.
끝이 날때까지 받아올 수 있게요.
public String recv( InputStream in ) throws Exception
{
if (in == null){
System.out.println(" - InputStream is null");
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int byteToBeRead = -1;
while((byteToBeRead = in.read())!=-1){
baos.write(byteToBeRead);
}
byte[] byData = baos.toByteArray();
baos.write(byData, 0,byData.length);
return new String(byData);
}
그런데 문제는 서버에서 out.flush를 해줘도 클라이언트에서는 데이터를 계속 기다리고있어요.
그래서 out.flush 후에 out.close를 해줬더니 서버 -> 클라이언트 데이터 전송은 됐는데.
클라이언트에서 서버로 데이터를 보내려고하니 소켓이 끊겼다고 하네요...
이틀째 디버깅중인데 도저히 답을 모르겠어서 염치 불구하고 소스를 가져옵니다 ㅠㅠ
서버 소스입니다.
public class SocketTestSample extends Thread
{
static int port = 8080; // server listen port
private Socket peer;
InputStream oIn = null;
OutputStream oOut = null;
public static void main(String[] args) throws Exception
{
ServerSocket server = new ServerSocket(port);
System.out.println("Server Start ( Port : " + port + " )" );
try
{
while (true)
{
Socket client = server.accept();
new SocketTestSample(client).start();
}
}
finally
{
try
{
if (server != null)
server.close();
}
catch (Exception e)
{
}
}
}
public SocketTestSample(Socket peer)
{
this.peer = peer;
}
public void run()
{
try
{
oIn = peer.getInputStream();
oOut = peer.getOutputStream();
String Msg = "Server Sample Test1";
while (peer != null && !peer.isClosed())
{
try
{
String recvString = recv(oIn);
System.out.println("====================== [recvString : "+recvString+"]=====================");
oOut.write(Msg.getBytes());
oOut.flush();
oOut.close();
peer.close();
peer = null;
icount++;
System.out.println("================통신 카운트 : "+icount + "=================\n");
}
catch (Exception e)
{
e.printStackTrace();
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (oIn != null)
oIn.close();
oIn = null;
}
catch (Exception e)
{
}
try
{
if (oOut != null)
oOut.close();
oOut = null;
}
catch (Exception e)
{
}
try
{
if (peer != null)
peer.close();
peer = null;
}
catch (Exception e)
{
}
}
}
public String recv( InputStream in ) throws Exception
{
if (in == null){
System.out.println(" - InputStream is null");
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int byteToBeRead = -1;
while((byteToBeRead = in.read())!=-1){
baos.write(byteToBeRead);
}
byte[] byData = baos.toByteArray();
baos.write(byData, 0,byData.length);
return new String(byData);
}
}
클라이언트입니다.
public void ClientTest( ) throws Exception
{
String MsgClient = "Client Test";
try
{
Socket sslsoc = new Socket( "120.15.31.14", 8080 );
out = sslsoc.getOutputStream();
in = sslsoc.getInputStream();
//3. 서버로 데이터 전송
byte[] sendMsg = MsgClient.getbyte();
out.write(sendMsg);
out.flush();
out.close();
bEncData=null;
// 4. 서버가 암호화해서 보내온 데이터 받아서 파일로 쓰기
byte[] recvData = recvByte(in);
System.out.println( "recvData" );
}
catch( Exception e)
{
e.printStackTrace( );
}
finally
{
try
{
if (in != null)
in.close();
}
catch (Exception e)
{
}
try
{
if (out != null)
out.close();
}
catch (Exception e)
{
}
try
{
if (sslsoc != null)
sslsoc.close();
}
catch (Exception e)
{
}
}
}
public byte[] recvByte( InputStream in ) throws Exception
{
if (in == null){
System.out.println(" - InputStream is null");
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedInputStream input = new BufferedInputStream(in);
byte[] binReadBuf = new byte[2048];
int byteToBeRead = -1;
while((byteToBeRead = input.read(binReadBuf)) !=-1){
baos.write(binReadBuf, 0, byteToBeRead);
}
byte[] byData = baos.toByteArray();
return byData;
}
}
왜 소켓이 끊기는걸까요 ㅠㅠㅠ