ダークホースプログラマーネットワークプログラミングのまとめ
5080 ワード
----------Java研修はあなたと交流することを期待しています。------
javaネットワークプログラミングのまとめ
TCP/IPプロトコルは、ネットワークを四層に分けた。アプリケーション層、トランスポート層、ネットワーク層、ネットワークインターフェース層。
TCPはトランスポート層にあり、IPはネットワーク層にある。TCPプロトコルは転送用であり、IPはアドレス指定用である。
Java.netパッケージには、重要なクラスを含めてInetAddressクラス、ServerSocket類、Socket類、Datagram Socket類、Datagram Packet類、URL類、URLConnection類があります。
InetAddressクラスは、コンピュータのIPアドレスとDNSをカプセル化するために使用されます。
DatagramPacketはデータウィンドウとして機能するクラスであり、Datagram SocketはDatagram Packetを送信および受信するための機構である。
TCP/IPセットは最も信頼できる双方向ストリームプロトコルであり、3回の握手で伝送の信頼性を確認し、クライアントを待つサーバはServerSocket類を使用し、サーバに接続するクライアントはソケット類を使用する。
URLはアドレスを使ってインターネット上のファイルを探すのを手伝っています。URLConnection類は汎用類であり、さらに遠隔資源を知るための方法を提供しています。
ソケットを利用してオブジェクトを転送することができ、性能の生産を解決するためによく使われる方法はソケットを通してストリームを送信することである。
具体的な実例コードは以下の通りです。
テキストファイル転送
サービス側は画像ファイルを同時に受信してアップロードします。
-------------------------------
javaネットワークプログラミングのまとめ
TCP/IPプロトコルは、ネットワークを四層に分けた。アプリケーション層、トランスポート層、ネットワーク層、ネットワークインターフェース層。
TCPはトランスポート層にあり、IPはネットワーク層にある。TCPプロトコルは転送用であり、IPはアドレス指定用である。
Java.netパッケージには、重要なクラスを含めてInetAddressクラス、ServerSocket類、Socket類、Datagram Socket類、Datagram Packet類、URL類、URLConnection類があります。
InetAddressクラスは、コンピュータのIPアドレスとDNSをカプセル化するために使用されます。
DatagramPacketはデータウィンドウとして機能するクラスであり、Datagram SocketはDatagram Packetを送信および受信するための機構である。
TCP/IPセットは最も信頼できる双方向ストリームプロトコルであり、3回の握手で伝送の信頼性を確認し、クライアントを待つサーバはServerSocket類を使用し、サーバに接続するクライアントはソケット類を使用する。
URLはアドレスを使ってインターネット上のファイルを探すのを手伝っています。URLConnection類は汎用類であり、さらに遠隔資源を知るための方法を提供しています。
ソケットを利用してオブジェクトを転送することができ、性能の生産を解決するためによく使われる方法はソケットを通してストリームを送信することである。
具体的な実例コードは以下の通りです。
テキストファイル転送
/* */
public class FileClient {
public static void main(String[] args) throws Exception, IOException {
Socket s = new Socket("localhost" , 10001);
BufferedReader bufr = new BufferedReader(new FileReader(
"FileServer.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while ((line = bufr.readLine()) != null) {
out.println(line);
}
s.shutdownOutput(); // , -1
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String serverResult = bufIn.readLine();
System. out.println(serverResult);
s.close();
}
}
//
public class FileServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10001);
Socket client = ss.accept();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(client
.getInputStream()));
System. out.println("ip:" +client.getInetAddress().getHostAddress());
PrintWriter pw = new PrintWriter(new FileWriter("save.txt"), true);
String line = null;
while ((line = bufIn.readLine()) != null) {
System. out.println(line);
pw.println(line);
}
PrintWriter out = new PrintWriter(client.getOutputStream());
out.println( " " );
out.close();
pw.close();
client.close();
ss.close();
}
}
-------------------------------サービス側は画像ファイルを同時に受信してアップロードします。
-------------------------------
/*
*
* :
* */
public class ConcurrentPicClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost" , 10001);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
//
FileInputStream is = new FileInputStream(new File("test.gif" ));
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
out.write(buf, 0, len);
}
s.shutdownOutput(); //
len = in.read(buf);
System. out.println(new String(buf, 0, len));
is.close();
s.close();
}
}
/*
*
* :
* Stream , Reader/Writer
* */
public class ConcurrentServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10001);
while (true ) {
new Thread(new ServerThread(ss.accept())).start();
}
}
}
class ServerThread implements Runnable {
private Socket s ;
public ServerThread(Socket s) {
this.s = s;
}
public void run() {
int count = 1;
String ip = s.getInetAddress().getHostAddress();
System. out.println(ip + "...connected" );
try {
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// file
File file = new File(ip + "(" + count + ").jpg");
while (file.exists()) {
count++;
file = new File(ip + "(" + count + ").jpg");
}
//
FileOutputStream fo = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
fo.write(buf, 0, len);
}
out.write( " " .getBytes());
fo.close();
s.close();
} catch (Exception e) {
}
}
}