新手入门,照着课本敲的,自己加的注释,若有错误,敬请指出。
接着会自己试着写多线程的、异地交流的一些东东。
前两个代码是客户机发话,服务器傻逼式回应;后面两个代码是客户机和服务器都是人工的,当线程的,轮流输入通信。
运行情况:
SERVER:
服务器启动
New connection accepted/127.0.0.1:2924
1
2
3
4
5
dxgjdfjg
bye]
bye
CLIENT:
How are you ! I am amy . you can talk with me .
1
Amy:YES!
2
Amy:I love you!
3
Amy:I love xiaopan!
4
Amy:Sorry I can't understand you!
5
Amy:Sorry I can't understand you!
dxgjdfjg
Amy:Sorry I can't understand you!
bye]
Amy:Sorry I can't understand you!
bye
Amy:See You!
【SERVER】
package socketserver; /** * EchoServer Server program * can do easy talk with Client * @author Amy * make in 2014/2 * @Version 1.0 */ import java.io.*; import java.net.*; public class EchoServer { //服务端程序 private int port = 8000; //链接端口 private ServerSocket serverSocket; public EchoServer() throws IOException { //开启服务端程序 serverSocket = new ServerSocket(port); //指定通信端口并开启服务 System.out.println("服务器启动"); } public String echo(String msg) { //给恢复信息加前缀 return "Amy:" + msg; } private PrintWriter getWriter(Socket socket) throws IOException { //给客户机回复交流语句 OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) throws IOException { //用于读取客户机交流语句 InputStream scoketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(scoketIn)); } public void server() { //服务程序 Socket socket = null; //清空客户机连接 try { socket = serverSocket.accept();// 等待客户机连接 System.out.println("New connection accepted" + socket.getInetAddress() + ":" + socket.getPort()); //客户机连接成功,给出新连接客户机IP地址及连接端口号 BufferedReader br = getReader(socket); //读取客户机交流语句 PrintWriter pw = getWriter(socket); pw.println("How are you ! I am amy . you can talk with me ."); //交流问候语 String msg = null; //清空对话缓存内容 while ((msg = br.readLine()) != null) { //读取客户机交流语句 若不为空 System.out.println(msg); //输出到服务端控制台 if (msg.equals("bye"))// 如果客户发送信息为“bye”,结束通信 { pw.println(echo("See You!")); //给客户机恢复对应语句//要结束服务了:下次再见 break; //结束服务 断开连接 } else if(msg.equals("1")) //与客户机交流 { pw.println(echo("YES!")); } else if(msg.equals("2")) { pw.println(echo("I love you!")); } else if(msg.equals("3")) { pw.println(echo("I love xiaopan!")); } else { pw.println(echo("Sorry I can't understand you!")); } } } catch (IOException e) { //异常 e.printStackTrace(); } finally { try { if (socket != null) //客户机关闭客户端 socket.close();// 断开连接 } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { //main new EchoServer().server(); } }
【CLIENT】/** * EchoClient Client program * can talk with Server * @author Amy * make in 2014/2 * @Version 1.0 */ import java.io.*; import java.net.*; import java.util.*; public class EchoClient { //客户端 private String host = "localhost"; //与本地服务端器通信 private int port = 8000; //指定通信端口 private Socket socket; public EchoClient() throws IOException { //与服务端建立连接,收到“问候语”表示连接成功 socket = new Socket(host, port); //与指定的服务器的指定端口连接 } private PrintWriter getWriter(Socket socket) throws IOException { //给服务端发送回话语句 OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) throws IOException { //接收服务端的会话语句 InputStream socketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(socketIn)); } public void talk()throws IOException{ //与服务端回话程序 try{ BufferedReader br=getReader(socket); //接收服务端的会话语句 PrintWriter pw=getWriter(socket); //给服务端发送回话语句 BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));//本地控制台输入 String msg=null; //清空语句缓存 System.out.println(br.readLine()); //输出服务端会话语句到服务台,连接成功的“问候语句” while((msg=localReader.readLine())!=null){ //读取到本地客户端控制台语句,不为空 pw.println(msg); //给服务端发送本地客户端控制台输入的会话语句 System.out.println(br.readLine()); //将接收到的服务端发来的会话语句输出到本地客户端控制台 if(msg.equals("bye"))break; } } catch(IOException e){ e.printStackTrace(); } finally { try{ socket.close(); //断开连接 } catch(IOException e){ e.printStackTrace(); } } } public static void main(String[] args) throws IOException{ //main new EchoClient().talk(); } }
自主对话:
【Server】
/** * EchoServer Server program * user can talk with Client * @author Amy * make in 2014/2 * @Version 1.1 */ import java.io.*; import java.net.*; public class EchoServer { //服务端程序 private int port = 8000; //链接端口 private ServerSocket serverSocket; public EchoServer() throws IOException { //开启服务端程序 serverSocket = new ServerSocket(port); //指定通信端口并开启服务 System.out.println("服务器启动"); } public String echo(String msg) { //给恢复信息加前缀 return "Amy:" + msg; } private PrintWriter getWriter(Socket socket) throws IOException { //给客户机回复交流语句 OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) throws IOException { //用于读取客户机交流语句 InputStream scoketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(scoketIn)); } public void server() { //服务程序 Socket socket = null; //清空客户机连接 try { socket = serverSocket.accept();// 等待客户机连接 System.out.println("New connection accepted" + socket.getInetAddress() + ":" + socket.getPort()); //客户机连接成功,给出新连接客户机IP地址及连接端口号 BufferedReader br = getReader(socket); //读取客户机交流语句 PrintWriter pw = getWriter(socket); //将会话内容发送给客户端 BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));//本地控制台输入 pw.println("How are you ! I am amy . you can talk with me ."); //交流问候语 String msg = null; //清空对话缓存内容 while ((msg = br.readLine()) != null) { //读取客户机交流语句 若不为空 System.out.println(msg); //输出到服务端控制台 if (msg.equals("bye"))// 如果客户发送信息为“bye”,结束通信 { pw.println(echo("See You!")); //给客户机恢复对应语句//要结束服务了:下次再见 break; //结束服务 断开连接 } else { msg=localReader.readLine();//读取本地服务端控制台内容 pw.println(echo(msg)); } /*else if(msg.equals("1")) //与客户机交流 { pw.println(echo("YES!")); } else if(msg.equals("2")) { pw.println(echo("I love you!")); } else if(msg.equals("3")) { pw.println(echo("I love xiaopan!")); } else { pw.println(echo("Sorry I can't understand you!")); }*/ } } catch (IOException e) { //异常 e.printStackTrace(); } finally { try { if (socket != null) //客户机关闭客户端 socket.close();// 断开连接 } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { //main new EchoServer().server(); } }
【CLIENT】
/** * EchoClient Client program * user can talk with Server * @author Amy * make in 2014/2 * @Version 1.1 */ import java.io.*; import java.net.*; import java.util.*; public class EchoClient { //客户端 private String host = "localhost"; //与本地服务端器通信 private int port = 8000; //指定通信端口 private Socket socket; public EchoClient() throws IOException { //与服务端建立连接,收到“问候语”表示连接成功 socket = new Socket(host, port); //与指定的服务器的指定端口连接 } private PrintWriter getWriter(Socket socket) throws IOException { //给服务端发送回话语句 OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } private BufferedReader getReader(Socket socket) throws IOException { //接收服务端的会话语句 InputStream socketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(socketIn)); } public void talk()throws IOException{ //与服务端回话程序 try{ BufferedReader br=getReader(socket); //接收服务端的会话语句 PrintWriter pw=getWriter(socket); //给服务端发送回话语句 BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));//本地控制台输入 String msg=null; //清空语句缓存 System.out.println(br.readLine()); //输出服务端会话语句到服务台,连接成功的“问候语句” while((msg=localReader.readLine())!=null){ //读取到本地客户端控制台语句,不为空 pw.println(msg); //给服务端发送本地客户端控制台输入的会话语句 System.out.println(br.readLine()); //将接收到的服务端发来的会话语句输出到本地客户端控制台 if(msg.equals("bye"))break; } } catch(IOException e){ e.printStackTrace(); } finally { try{ socket.close(); //断开连接 } catch(IOException e){ e.printStackTrace(); } } } public static void main(String[] args) throws IOException{ //main new EchoClient().talk(); } }