文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Java 在学习二十五【原创】  


Java网络编程


1java.net.InetAddress;

2java.net.ServerSocket;

3java.net.Socket;


计算机有65536个端口,1024以上;

客户端;

服务端;


Server.java

package SocketTest;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;


public class Server {


public static void main(String[] args) {

// TODO Auto-generated method stub

try {

//开启服务

ServerSocket s = new ServerSocket(60001);

//等待客户来访问

System.out.println("服务端已经开启");

Socket socket = s.accept();

System.out.println("接受到:" + socket.getInetAddress().getHostAddress() + "的访问");

BufferedReader br = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

BufferedReader br1 = new BufferedReader(

new InputStreamReader(System.in));

PrintWriter out = new PrintWriter(socket.getOutputStream(), true); //从键盘读数据

// String str = br.readLine();

// System.out.println(str);

//socket.close();

//s.close();

//while(true) {

new ReadThread(br).start();

new WriteThread(br1, out).start();

//}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}



client.java

package SocketTest;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;


public class Client {


public static void main(String[] args) {

// TODO Auto-generated method stub

try {

Socket socket = new Socket("localhost", 60001);

System.out.println("客户端连接服务器");

BufferedReader br = new BufferedReader(

new InputStreamReader(System.in));

//String str = br.readLine();

//socket.getOutputStream();

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader br1 = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

//out.println(str);

//socket.close();

//while(true) {

new WriteThread(br, out).start();

new ReadThread(br1).start();

//}

} catch (UnknownHostException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


ReadThread

package SocketTest;


import java.io.BufferedReader;

import java.io.IOException;


public class ReadThread extends Thread {

private BufferedReader br;

public ReadThread(BufferedReader br) {

this.br = br;

}


@Override

public void run() {

String str;

try {

while (true) {

str = br.readLine();

System.out.println(str);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}



WriteThread

package SocketTest;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.PrintWriter;


public class WriteThread extends Thread {

private BufferedReader br;

private PrintWriter out;

public WriteThread(BufferedReader br, PrintWriter out) {

super();

this.br = br;

this.out = out;

}



@Override

public void run() {

String str;

try {

while (true) {

str = br.readLine();

out.println(str);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}