TCP/IPプログラミング

4287 ワード

ネットワークプログラミングとはペアです...互いにつながっている...soには2つのコードがあり、1つはクライアントで、1つはサービス側です.のこの2つのコードは最も簡単で、テンプレート型で、私の初心者にとって、感じは簡単ではありません
コードは足跡を残すためにここにあります.の私が深く踏んだことがある...
 
package heng.java.TCP;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
	public static void main(String[] args) {
		try {
			ServerSocket ss = new ServerSocket(8888);//  6666     ,         
			while(true){
				Socket s = ss.accept();
				DataInputStream dis = new DataInputStream(s.getInputStream());
				System.out.println(dis.readUTF());//   。(    )UTF    Client     
				dis.close();
				s.close();
				//System.out.println("A client connect");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 
 
package heng.java.TCP;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient {
	public static void main(String[] args) {
		try {
			//    
			Socket s = new Socket("192.168.1.102",8888);
			OutputStream os = s.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF("Hello server!");
			dos.flush();
			dos.close();
			s.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

=================================================================================
package heng.java.TCP1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTwoTalkServer {
	public static void main(String[] args) {
		InputStream in = null;
		OutputStream out = null;
		try {
			ServerSocket ss = new ServerSocket(5888);
			Socket socket = ss.accept();
			in = socket.getInputStream();
			out = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(out);
			DataInputStream dis = new DataInputStream(in);
			
			String s = null;
			if((s = dis.readUTF()) != null){
				System.out.println(s);
				System.out.println("from:"+socket.getInetAddress());
				System.out.println("Port:"+socket.getPort());
			}
			
			dos.writeUTF("hi,   !");
			dis.close();
			dos.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 
 
package heng.java.TCP1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPTwoTalkClient {
	public static void main(String[] args) {
		InputStream is = null;
		OutputStream os = null;
		try {
			Socket socket = new Socket("192.168.1.102",5888);
			is = socket.getInputStream();
			os = socket.getOutputStream();
			DataInputStream dis = new DataInputStream(is);
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF("hello");
			
			String s = null;
			if((s = dis.readUTF()) != null){
				System.out.println(s);
			}
			dos.close();
			dis.close();
			socket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}