TCP/IP通信のデータ送信で発生する問題とその解決方法

このQ&Aのポイント
  • TCP/IP通信において、データ送信時に発生する問題とその解決方法について説明します。
  • 現在、TCP/IP通信のプログラミングを行っており、サーバ/クライアント別々に4byteのデータ送信を10msec毎に10秒間行っていますが、データが連なって送信される事象が発生しています。
  • OutputStreamではwrite後にflushを行っているので、送信用バッファが送信されるイメージであるが、実際には4byteで送信できていないように見えます。解決方法をご教授ください。
回答を見る
  • ベストアンサー

TCP/IP通信について

現在、以下のようにTCP/IP通信のプログラミングを行っており、 サーバ/クライアント別々に4byteのデータ送信を10msec毎に10秒間行っております。 現在、WimdowsVista-Windows7間で各々をサーバ/クライアントとして順に起動し、 相互に4byte送信しているハズが、倍の8byteや12byteとデータが連なって送信されている 事象が発生してます。 OutputStreamではwrite後にflushを行っているので、flush契機でメモリ上に蓄えられた 送信用バッファが送信されるイメージでおりますが、4byteで送信できていないように見えます。 上記について、解決方法をご存じであればご教授お願い致します。   <Server.java> ===== public class Server {  public static ServerSocket ss = null;  public static Socket soc = null;  private static InputStream is = null;  private static OutputStream os = null;  public static void main(String[] args) {      try {    // サーバソケット生成    ss = new ServerSocket(5000);    soc = ss.accept();    is = soc.getInputStream();    os = soc.getOutputStream();    Thread rcvTh = new ServerRcvThread(is);    rcvTh.start();    Thread sndTh = new ServerSndThread(os);    sndTh.start();    // 10秒スリープ    try{     Thread.sleep(10000);    } catch ( Exception e){     e.printStackTrace();    }    // スレッド停止    rcvTh.stop();    sndTh.stop();   } catch (IOException e) {    e.printStackTrace();   } finally{    try {     is.close();     os.close();     soc.close();     ss.close();    } catch (IOException e) {     e.printStackTrace();    }      }  } } class ServerSndThread extends Thread{  private static OutputStream ous = null;  ServerSndThread( OutputStream os ){   this.ous = os;  }  public void run(){   byte sndData[] = new byte[4];   sndData[0] = 0x01;   sndData[1] = 0x02;   sndData[2] = 0x03;   sndData[3] = 0x04;   try {    while(true){     // データ書込み     ous.write(sndData);     ous.flush();     System.out.println("データ送信");     // 0.01秒スリープ     try{      Thread.sleep(10);     } catch ( Exception e){      e.printStackTrace();     }    }   } catch (IOException e) {    e.printStackTrace();   }  } } class ServerRcvThread extends Thread{  private static InputStream ins = null;  ServerRcvThread( InputStream os ){   this.ins = os;  }    public void run(){   byte rcvData[] = new byte[16];   int size = 0;   try {    while(true){     // データ読込み     size = ins.read(rcvData);     System.out.println("size:"+size+"byte");    }   } catch (IOException e) {    e.printStackTrace();   }  } } =====     <Client.java> ===== public class Client {  private static Socket soc = null;  private static OutputStream os = null;  private static InputStream is = null;  public static void main(String[] args) {   try {    // ソケット生成    soc = new Socket("192.168.3.3", 5000);    is = soc.getInputStream();    os = soc.getOutputStream();    Thread rcvTh = new ClientRcvThread(is);    rcvTh.start();    Thread sndTh = new ClientSndThread(os);    sndTh.start();    // 10秒スリープ    try{     Thread.sleep(10000);    } catch ( Exception e){     e.printStackTrace();    }    // スレッド停止    rcvTh.stop();    sndTh.stop();   } catch (IOException e) {    e.printStackTrace();   } finally{    try {     is.close();     os.close();     soc.close();    } catch (IOException e) {     e.printStackTrace();    }      }     } } class ClientSndThread extends Thread{  private static OutputStream ous = null;  ClientSndThread( OutputStream os ){   this.ous = os;  }    public void run(){   byte sndData[] = new byte[4];   sndData[0] = 0x04;   sndData[1] = 0x03;   sndData[2] = 0x02;   sndData[3] = 0x01;   try {    while(true){     // データ書込み     ous.write(sndData);     ous.flush();     System.out.println("データ送信");     // 0.01秒スリープ     try{      Thread.sleep(10);     } catch ( Exception e){      e.printStackTrace();     }    }   } catch (IOException e) {    e.printStackTrace();   }  } } class ClientRcvThread extends Thread{  private static InputStream ins = null;  ClientRcvThread( InputStream os ){   this.ins = os;  }    public void run(){   byte rcvData[] = new byte[16];   int size = 0;   try {    while(true){     // データ読込み     size = ins.read(rcvData);     System.out.println("size:"+size+"byte");    }   } catch (IOException e) {    e.printStackTrace();   }  } } =====

  • Java
  • 回答数4
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
回答No.4

> なるほどー。境界が維持されないんですねー。 > ということは、パケット内にサイズを保持するとかしないといけないんですかね…。 そうなります。 TCPの機能はJava自体ではなく,OS等が実現しています。 パケット未達時に再送してくれたり,パケットの到着順が入れ替わった場合に並び替えるのもJavaではなくOS等の機能です。 着信系の機能はプロセスを横断して行う必要があるので,UNIXやWindowsといったOSではJavaに任せるのは難しいと思いますし。 # どこまでをOSと呼ぶかやデバイスドライバの実装によって実際の処理箇所が異なるのでOS等と書いています。 なので,Java側でflushしても,デバイス (デバイスドライバ含む) のバッファに転送されるだけかと思います。 さらにNagleアルゴリズムが有効な限り,最大で200ms程度 (規格上は500ms) の間の送信はデバイス側でまとめられます。 今回はACKを載せるパケットがどんどん出来るので,遅延ACKによる遅延がほとんどなく,結果としてNagleアルゴリズムも大きく働いてはいないのだと思います。 # サーバー側が送信を止めた場合,800octetくらいのデータが一気に送受信されるかもしれません。 送信側としては,Nagleアルゴリズムを無効にすることでデータを即座に送信することが出来ます。 ただし,受信側でパケット境界が維持されるとは限りませんし,4octetのデータに20octetのTCPヘッダと20octetのIPv4ヘッダ (IPv6だと40octet) が付随するので,ネットワーク効率を下げる可能性があります。

その他の回答 (3)

  • SeydaNeen
  • ベストアンサー率43% (26/60)
回答No.3

すみません、flushの説明違うかも。あと、4バイトより短いデータが取れるかもしれません。それはsizeを見て残りをとって・・。

  • SeydaNeen
  • ベストアンサー率43% (26/60)
回答No.2

UDPという意見もありますが、今から組み替えて、到達確認、再送処理を入れ込むのは面倒ですね。この辺、本番環境で問題になるんですよ。 と言いますか、 byte rcvData[] = new byte[16];とやってるのはなぜでしょうか?これは、「最大16バイト取ろう」としていることになります。 size = ins.read(rcvData, 0, 4);とやるのがいいと思いますが。 実際にこのプログラムを動かしてみましたよ。(javacとやったら、なんとjdkインストールしとらんかった!) 8バイト取ることがありましたが、改良後は100秒動かしても4以外はありません・・・。 ちなみに、「OutputStream の flush メソッドは何も行いません。」とAPIドキュメントに書かれてます。APIドキュメントは重要です。

shin2chan
質問者

お礼

回答ありがとうございます! サイズ指定しても、8byte となってしまいますね…。 改良って他にも変更されたのでしょうか…? flushのAPI見ましたが、何もしないとは書かれていないように見えました。 ただ、実際のAPIソースコード見たら、ご指摘通り何にもしない、空APIに見えました。

回答No.1

TCPはパケット境界を維持しないプロトコルです。 送信側での複数のパケットが受信側では1つのパケットになることもありますし, 送信側では1つのパケットだったものが受信側では複数のパケットとなることもあります。 そのため,パケット境界が維持されないことを前提にTCP上にプロトコルを作成するか,UDPなどのパケット境界を維持するプロトコルを使う必要があります。

shin2chan
質問者

お礼

回答ありがとうございます! なるほどー。境界が維持されないんですねー。 ということは、パケット内にサイズを保持するとかしないといけないんですかね…。

関連するQ&A

  • javaのsocket通信で20KB以上の文字列を

    javaのsocketについての質問です。サーバーとandroid端末で文字列のやりとりしています。 文字列は長いので、圧縮をかけ、android端末側で解凍を行っています。 そこで問題がおきました。文字データ量(20KB)が大きくなると、通信が完了せずに途中で止まってしまうのです。そこで文字列を分割(1KB毎)して送るなどを行い、しのいでおりましたが根本的の解決にはいたっていません。 InputStreamReader 、 BuffererdReader あたりを考えればいいと思うのですが、いまいち組み方がよくわからない状態です。 どなたか教えていただけないでしょうか? ============================= サーバー(PC側ソースコード)DataCompression は文字列を送って圧縮をかける独自のクラスです。 public class DataExchange extends Thread{ /* android端末とデータのやり取りをする */ private boolean DEBUG_FLAG = true; // private boolean DEBUG_FLAG = false; private InputStream is; private OutputStream os; private String sendStr; private String receiveStr; public DataExchange(InputStream is, OutputStream os, String sendStr) { this.is = is; this.os = os; this.sendStr = sendStr; } public void run() { //送信データ圧縮 DataCompression dc = new DataCompression(); byte[] sendByte = dc.getDataCompression(sendStr); //データ送信 try { os.write(sendByte); os.flush(); } catch (IOException e) { e.printStackTrace(); } if (DEBUG_FLAG) { System.out.println(os + " データ送信完了 = " + sendStr); } //データ受け取り byte[] buffer = new byte[1024]; try { int size = 0; while (size <= 0) { size = is.read(buffer); } receiveStr = new String(buffer, 0,size, "UTF8"); } catch (IOException e) { e.printStackTrace(); } if (DEBUG_FLAG) { System.out.println(is + " データ受信完了 = " + receiveStr); } } public String getReceiveStr() { return receiveStr; } } ========================================= android端末側ソースコード public class DataExchange extends Thread{ /* android端末とデータのやり取りをする */ private InputStream is; private OutputStream os; private String sendStr; private String receiveStr; public DataExchange(InputStream is, OutputStream os, String sendStr) { this.is = is; this.os = os; this.sendStr = sendStr; } public void run() { //データ受け取り byte[] buffer = new byte[4096]; try { int size = 0; while (size <= 0) { size = is.read(buffer); } receiveStr = new String(buffer, 0,size, "UTF8"); } catch (IOException e) { e.printStackTrace(); } /* 解凍 */ Inflater decompresser = new Inflater(); decompresser.setInput(buffer); int count; ByteArrayOutputStream decompos = new ByteArrayOutputStream(); while (!decompresser.finished()) { try { count = decompresser.inflate(buffer); decompos.write(buffer, 0, count); } catch (DataFormatException e) { e.printStackTrace(); } } receiveStr = decompos.toString(); System.out.println("データ受信完了 = " + receiveStr); //データ送信 try { byte[] sendByte = sendStr.getBytes("UTF8"); os.write(sendByte); os.flush(); } catch (IOException e) { e.printStackTrace(); } System.out.println("データ送信完了 = " + sendStr); } public String getReceiveStr() { return receiveStr; } }

    • ベストアンサー
    • Java
  • 初歩的なソケット通信(java)

    初歩的なソケット通信(java) javaのソケット通信プログラムについて質問させて頂きます。まだまだ基礎の段階なのですが、詰まってしまたので、良かったら教えてください。 以下プログラムコード ------------------------------------- //サーバー側 import java.net.*; import java.io.*; public class Server { static int port = 12345; public static void main(String[] args) { try{ //サーバソケットの作成 ServerSocket ssoc = new ServerSocket(port); //メインループ while(true){ try{ System.out.println("クライアントからの接続を"+ port +"で待ちます"); Socket soc = ssoc.accept(); System.out.println(soc.getInetAddress().getHostName()+"から接続を受けました"); //処理をスレッドに任せます new Connect(soc); }catch(IOException e1){ e1.printStackTrace(); } } }catch(IOException e2){ e2.printStackTrace(); } } } class Connect extends Thread{ private Socket socket = null; public Connect(Socket socket){ this.socket = socket; //スレッド開始 this.start(); } public void run(){ try{ //出力ストリームを取得 OutputStream os = socket.getOutputStream(); PrintWriter out = new PrintWriter(os,true); //入力ストリームを取得 InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader in = new BufferedReader(isr); String inputLine; while((inputLine = in.readLine()) != null){ //System.out.println(inputLine); out.println(inputLine); } System.out.println("処理が終わったので接続を切ります"); in.close(); out.close(); socket.close(); }catch(Exception e1){ try{ socket.close(); }catch(Exception e2){ e2.printStackTrace(); } } } }

  • URLで指定したページのデータを読むには?

    URLをしていしてデータを読み込みたいのですが”java.net.ConnectException: Connection timed out”となってしまいます。なぜでしょうか。 import java.io.*; import java.net.*; /** * ウェブ上のデータを取得するサンプル * */ public class GetFileFromWeb { public static void main(String[] args){ URL url = null; //HttpURLConnection con = null; try{ //URLの指定 url = new URL("http://www.****"); //入力ストリームを取得する InputStream is = url.openStream(); //URLからデータを読み取り、表示する。 byte[] data = new byte[1024]; int i; while((i=is.read(data))!=-1){ System.out.write(data, 0, i); } }catch(MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

    • ベストアンサー
    • Java
  • java でソケット通信をするとき

    javaでソケット通信をするとき、 //データ受け取り byte[] buffer = new byte[2048]; try { int size = 0; while (size <= 0) { size = is.read(buffer); } receiveStr = new String(buffer, 0,size, "UTF8"); } catch (IOException e) { e.printStackTrace(); } のようにしていますが、これだと文字数にして一度におくれる容量はどのくらいでしょうか? また、それらを増やすにはどうしたらいいでしょうか?

    • ベストアンサー
    • Java
  • TCP関係のjavaプログラム

    TCPでオウム返しをするサーバとクライアントソフトでクライアント側でピリオドだけを入力すると接続を切るプログラムを作っているのですがなかなか思い通りにいきません。 正しいソースコードを教えてください。 クライアント側のソースコード import java.io.*; import java.net.*;   public class echoClient { public static void main(String[] args) { Socket echoSocket = null; DataOutputStream os = null; BufferedReader is = null;   try { echoSocket = new Socket("localhost", 9999); os = new DataOutputStream(echoSocket.getOutputStream()); is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: localhost"); }   if (echoSocket != null && os != null && is != null) { try { os.writeBytes("HELLO\n");   String responseLine; if ((responseLine = is.readLine()) != null) { System.out.println("Server: " + responseLine); }   os.close(); is.close(); echoSocket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { System.err.println("IOException: " + e); } } } } サーバー側のプログラム import java.io.*; import java.net.*; public class echoServer { public static void main(String args[]) { ServerSocket echoServer = null; String line; BufferedReader is; PrintStream os; Socket clientSocket = null; try { echoServer = new ServerSocket(9999); } catch (IOException e) { System.out.println(e); } try { clientSocket = echoServer.accept(); is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); os = new PrintStream(clientSocket.getOutputStream()); while (true) { line = is.readLine(); os.println(line); } } catch (IOException e) { System.out.println(e); } }

  • javaについて

    このプログラムは文字を入力して、検索をかけてその検索にかかった秒数を表示するものですが、検索数を表示するのにはどうしたらいいでしょうか? import java.io.*; import java.net.*; public class Sample { public static void main(String[] args) { System.out.println("入力してください"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); double sec = getSearchSec(htmlSrc); if (sec > 0) { System.out.println("検索秒数は" + sec + "秒"); } else { System.out.println("検索結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static double getSearchSec(String htmlSrc) { double sec = 0; String tmp = htmlSrc.replaceAll("<.+?>| ", ""); tmp = tmp.replaceAll(".*件-", ""); tmp = tmp.replaceAll("秒.*", ""); try { sec = Double.parseDouble(tmp); } catch (NumberFormatException e) { } return sec; } }

  • javaについて

    このプログラムは文字を入力して、検索をかけてその検索にかかった秒数を表示するものですが、検索数を表示するのにはどう書き換えますでしょうか? import java.io.*; import java.net.*; public class Sample { public static void main(String[] args) { System.out.println("入力してください"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); double sec = getSearchSec(htmlSrc); if (sec > 0) { System.out.println("検索秒数は" + sec + "秒"); } else { System.out.println("検索結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static double getSearchSec(String htmlSrc) { double sec = 0; String tmp = htmlSrc.replaceAll("<.+?>| ", ""); tmp = tmp.replaceAll(".*件-", ""); tmp = tmp.replaceAll("秒.*", ""); try { sec = Double.parseDouble(tmp); } catch (NumberFormatException e) { } return sec; } }

    • ベストアンサー
    • Java
  • ファイルの書き出しについて、どちらのほうが良い方法でしょうか?

    下の2つのメソッドでどちらのほうがよいでしょうか? 実行時間的にはuseStringのほうが若干速いのですが... メモリ消費とかセオリーとか常識とかそういうのを含めて解説を頂きたいです。 今のところ結果は同じものが出力されています。 public static void useString() { try { System.out.println("start useString"); URL url = new URL("どこかのページ"); File outputFile = new File("test1.html"); InputStream input = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile.toString(), true)); String tmp = ""; String str; while ((str = reader.readLine()) != null) { writer.write(tmp); } reader.close(); input.close(); writer.write(tmp); writer.close(); } catch (IOException e) { e.printStackTrace(System.out); } } public static void useStream() { try { URL url = new URL("どこかのページ"); File outputFile = new File("test.html"); InputStream input = url.openStream(); FileOutputStream output = new FileOutputStream(outputFile); byte[] bytes = new byte[2048]; int n; while ((n = input.read(bytes)) >= 0) { output.write(bytes, 0, n); } input.close(); output.close(); } catch (IOException e) { e.printStackTrace(System.out); } }

    • ベストアンサー
    • Java
  • java 検索数表示ができるようにしたいのですが

    補足へ import java.io.*; import java.net.*; import java.lang.*; public class secc3 { public static void main(String[] args) { System.out.println("入力"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); String sec = getNumSearchHits(htmlSrc); sec = sec.replace(",",""); int ken = Integer.parseInt(sec); System.out.println(ken); if (ken > 0) { System.out.println("Hit数約" + ken + "件"); } else { System.out.println("結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static String getNumSearchHits(String htmlSrc) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(" / 約([,0-9]+)件 - "); java.util.regex.Matcher m = p.matcher(htmlSrc); return m.find() ? htmlSrc.substring(m.start(1), m.end(1)) : ""; } }

  • 403を返されてしまいます。

    グーグルの検索結果を取得したいのですが 以下のようなプログラムでは403を返されてしまいます。 他のページではちゃんと取得できるのですが 原因は何なんでしょうか。また、これを回避する方法はあるのでしょうか? import java.net.*; import java.io.*; public class test { public static void main(String args[]) { try { URL objURL = new URL("http://www.google.co.jp/search?hl=ja&q=java&lr="); URLConnection objURLCnn = objURL.openConnection(); InputStream inputStream = objURLCnn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "JISAutoDetect")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.co.jp/search?hl=ja&q=java&lr= at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at test.main(test.java:10)

専門家に質問してみよう