2014-05-20 3 views
0

몇 가지 이유 때문에 서버 소켓을 통해 여러 바이트 배열을 개별적으로 보내야하며 클라이언트 소켓은 이러한 바이트 배열을 수신합니다. 바이트 배열을 보낸 후 클라이언트 소켓에 수신 된 바이트 배열이 서버 소켓에있는 바이트 배열과 같지 않다는 것을 알았습니다. ObjectOutputStream과 ObjectInputStream을 사용하면 모든 것이 정상적으로 작동하지만 내 필요에 따라 ObjectOutputStream & ObjectInputStream을 사용할 수 없습니다. 서버가 2 개 이상의 소켓을 연결해야하기 때문입니다.dataoutputstream을 통해 바이트 배열을 보낸 후 수신 바이트 배열이 원래 바이트 배열과 일치하지 않습니다.

서버 (발신자) :

private void sendData() throws IOException { 
     DataOutputStream outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); 

     File file = new File(filePath); 
     byte[] fileByte = new byte[(int) file.length()]; 

     // Convert file into byte array 
     FileInputStream fileInputStream = new FileInputStream(file); 
     fileInputStream.read(fileByte); 

     while (!decoder.isDecoded()) { 
      byte[] packet = new byte[encoder.getPacketLength()]; 
      encoder.getPacket(packet, 0); 

      if (Math.random() < 0.1) 
       continue; 

      // Send data HERE!!! 
      outputStream.writeInt(encoder.getPacketLength()); 
      outputStream.write(packet); 

      StringBuilder sb = new StringBuilder(); 
      for (byte value : packet) { 
       sb.append(value); 
      } 
      serverLog("Packet content= " + sb.toString()); 

      decoder.addPacket(packet, 0); 
     } 

     outputStream.close(); 
     outputStream.flush(); 
     fileInputStream.close(); 
    } 

클라이언트 (수신기)

Packet content= 8312654-7646-55-71-522462106-89115-98-107-66-9445-61-78-99-43-52528422-10765-3719-1243-114-115-5869-119-92-113591131276057-905042126-7557940-2850118-9737-6512759-93-393-89-96104-789-7694747-118-61-79-97-872-4369-82-11923-805-62-9527-38-1195-13107673425588-92-49-44-97-7811812416-781244-100 
Packet content= -10295-4-73-809789-87-97-9095-55-11-7440100-2871-89-68-59-3739-2073-1286290-21-106-2-25-1263646-107-84-9-42-4491-418419-469395-61-52-32-85-38-107-8385-6559-1286575-56-1029-5991-7551-10390-25-94-5072-2318-62-12360-818-11719-73710076121-584012212645-20-170-45089-87401082110113-7-1034873-877 
Packet content= -37-732-59-1071015223-34-2745-4322265-18-108-97126-22-56083-131-61-16069536-10-7092-36-50-96-72-3133-79-11751-3671-91-48-14-54-113-8295-124-18-78125-117-12811911042-34108-94-33-92-113268272-41-23992553-69-72997619-1166735417-76-126-58121-7595-443265-944-44-124104-201040-803310126-125-8449-8 
Packet content= -102110606194-6037-2-63-6025-112-17-36-3629-93112-86-116-54-14118-124121315341-11923-119-12246-1-973-31-22-9-111-12614-37-1456-10456-9450-11823811378-779-111101-31-101-843217-124-16107-108107101-7743-108-100-5475113119-6647-7185-96-683-12710756-9896111-1072318-3982-6288-7496-5514-18373957-71108-3232 
Packet content= -6689-124263-68321112-19-6730-68-12223-125105-51444110862-11063-114-41-76-5275-7-15105-865826-5049-12386124123-121-10460-863108-65-481142910461-97-11912453-10718-6711787-91-37-97910-22-1963-88-116114-138375-2082027-128126-8529-3632242-772-105106-35-41-44-573126-7512038-62-733838-434433-9-59 
Packet content= -9652035246-1482-59-63699991-47-25-2-15127-55-42-97-106-11966-15-4344462-68-2-916794121-31-11164-40-73-8104342-69-38-122986-11584-57-106-35-49-120-15138-19121-32-88-62-5127-95114-26-34-75-377155-6911914-4210849-116-7028-8180-817585-108-76-3337-21-4589108-121794-85109-6810644-102101106-68-5-127 
Packet content= -127-124-61-24479910112178-45-47-952540-122-70100-120-17-5114-127-69-12-38-100-111-21-111-44113-10-12490-72884-9549-1115580-7653-16100-7889-22103-747-476698-9-68-83-301112667-1712127-64-45-746-1054497-7071108-109-9-8946-4259-121-106-121-27-6761-3447-1177476-7271-95-75-107-42-331886-119-10578-6098-128-2287-52 
Packet content= 5244-100-68-1215109-27-6810-774491-11-29102-6267-621964-975427701107495-33578037-1924-5611657210-46-16-94-53-92-5282-11353-10359-38-1-385212480-12710119-597634-108-67-49109101-69-7933-10189120-76104-46106-64223-44-10-4789-1213910192-125-60-5-20-37-901-5-104103-739314-4414-1123759-31-45-72 
Packet content= 119-4161558-7832-8310329-118-91122-85-37-33-30-52-101087-123784011115-57-31-1166-31-55-43-13-49-5812464-16-1382-30-3810445117659-70810584-46113-11-5111-1251-428299112-816596-1091021353-4679-3526-10363100-10-4-318-49-117-10449103105-11190-94-5310633-2-123-5268-119124-12297-106-114-102110-35-12094-128 
Packet content= -381-70-109-26112-2552-92-40-39-22966-36-69-118-116-19-4848-9-12869-102-4558-2198362-121-553776-84-118-93125-12374128555-8688-2-729-43126-3-10083-26-64119-6010577-12263-4-74-44-11028-216447727-5082-104100-3410673529-8970-27110-81-90-1221185-22-249510515-123-157817-43-11961-93141-8512-77-7352 
: 서버

public void receiveData() throws UnknownHostException, IOException, ClassNotFoundException { 
    in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

    DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
    int len = dis.readInt(); 

    while (!decoder.isDecoded()) { 
     byte[] packet = new byte[len]; 

     dis.read(packet, 0, len); 

     StringBuilder sb = new StringBuilder(); 
     for (byte value : packet) { 
      sb.append(value); 
     } 
     messageArea.append("Packet content= " + sb.toString() + "\n"); 

     decoder.addPacket(packet, 0); 
    } 
} 

로그 여기

서버 및 클라이언트 코드클라이언트에서 363,210

로그 :

Packet content= 8312654-7646-55-71-522462106-89115-98-107-66-9445-61-78-99-43-52528422-10765-3719-1243-114-115-5869-119-92-113591131276057-905042126-7557940-2850118-9737-6512759-93-393-89-96104-789-7694747-118-61-79-97-872-4369-82-11923-805-62-9527-38-1195-13107673425588-92-49-44-97-7811812416-781244-100 
Packet content= 000110-10295-4-73-809789-87-97-9095-55-11-7440100-2871-89-68-59-3739-2073-1286290-21-106-2-25-1263646-107-84-9-42-4491-418419-469395-61-52-32-85-38-107-8385-6559-1286575-56-1029-5991-7551-10390-25-94-5072-2318-62-12360-818-11719-73710076121-584012212645-20-170-45089-87401082110113-7-103 
Packet content= 4873-877000110-37-732-59-1071015223-34-2745-4322265-18-108-97126-22-56083-131-61-16069536-10-7092-36-50-96-72-3133-79-11751-3671-91-48-14-54-113-8295-124-18-78125-117-12811911042-34108-94-33-92-113268272-41-23992553-69-72997619-1166735417-76-126-58121-7595-443265-944-44-124104-201040 
Packet content= -803310126-125-8449-8000110-102110606194-6037-2-63-6025-112-17-36-3629-93112-86-116-54-14118-124121315341-11923-119-12246-1-973-31-22-9-111-12614-37-1456-10456-9450-11823811378-779-111101-31-101-843217-124-16107-108107101-7743-108-100-5475113119-6647-7185-96-683-12710756-9896111-1072318-3982-6288 
Packet content= -7496-5514-18373957-71108-3232000110-6689-124263-68321112-19-6730-68-12223-125105-51444110862-11063-114-41-76-5275-7-15105-865826-5049-12386124123-121-10460-863108-65-481142910461-97-11912453-10718-6711787-91-37-97910-22-1963-88-116114-138375-2082027-128126-8529-3632242-772-105106-35-41 
Packet content= -44-573126-7512038-62-733838-434433-9-59000110-9652035246-1482-59-63699991-47-25-2-15127-55-42-97-106-11966-15-4344462-68-2-916794121-31-11164-40-73-8104342-69-38-122986-11584-57-106-35-49-120-15138-19121-32-88-62-5127-95114-26-34-75-377155-6911914-4210849-116-7028-8180-817585-108-76 
Packet content= -3337-21-4589108-121794-85109-6810644-102101106-68-5-127000110-127-124-61-24479910112178-45-47-952540-122-70100-120-17-5114-127-69-12-38-100-111-21-111-44113-10-12490-72884-9549-1115580-7653-16100-7889-22103-747-476698-9-68-83-301112667-1712127-64-45-746-1054497-7071108-109-9-8946-4259-121-106-121-27-67 
Packet content= 61-3447-1177476-7271-95-75-107-42-331886-119-10578-6098-128-2287-520001105244-100-68-1215109-27-6810-774491-11-29102-6267-621964-975427701107495-33578037-1924-5611657210-46-16-94-53-92-5282-11353-10359-38-1-385212480-12710119-597634-108-67-49109101-69-7933-10189120-76104-46106-64223-44 
Packet content= -10-4789-1213910192-125-60-5-20-37-901-5-104103-739314-4414-1123759-31-45-72000110119-4161558-7832-8310329-118-91122-85-37-33-30-52-101087-123784011115-57-31-1166-31-55-43-13-49-5812464-16-1382-30-3810445117659-70810584-46113-11-5111-1251-428299112-816596-1091021353-4679-3526-10363100 
Packet content= -10-4-318-49-117-10449103105-11190-94-5310633-2-123-5268-119124-12297-106-114-102110-35-12094-128000110-381-70-109-26112-2552-92-40-39-22966-36-69-118-116-19-4848-9-12869-102-4558-2198362-121-553776-84-118-93125-12374128555-8688-2-729-43126-3-10083-26-64119-6010577-12263-4-74-44-11028-216447727-5082 

모두, 어떻게이 문제를 방지하려면 다음 코드를 편집하고 전송 된 패킷을 어떻게 보장 할 첫 번째 패킷 후 수신 된 패킷에 삽입 6의 정수 "000110"은 항상있다 볼 수 있습니다 수신 된 패킷이 동일합니까? 도와주세요, 친절한 도움에 감사드립니다. :)

+2

당신은 모든 데이터가'read()'를 한 번 호출 할 때 읽힌다고 가정합니다. 일반적으로 스트림에 대한 안전한 가정은 아닙니다. (그것은 * DataInputStream을위한 * 가능 *하지만 그것은 아닌 것 같아요.) * 항상 * 얼마나 많은 데이터가 실제로 읽었는지 알아 내기 위해'read'의 반환 값을 사용해야하고, 당신이 기대하는 모든 것을 읽었습니다. –

+0

서버 측에서 모든 패킷에 대해 packt 길이를 보내고 클라이언트 측에서는 길이를 한 번만 읽습니다. 모든 패킷의 길이를 읽지 않아야합니까? 또한 전체 패킷을 읽는 것에 대한 이전 설명을 고려하십시오. –

+0

일반적으로 모든 것이 전송되었는지 확인하기 위해 쓰기 후에 데이터 출력 스트림을 flush()합니다. –

답변

0

반환 값을 확인하지 않고 read()이 버퍼를 채웠다고 가정합니다. 최소한 1 바이트 만 전송하도록 지정되어 있지 않습니다. 해당 동작을 얻으려면 readFully()을 사용하십시오.

관련 문제