首页 > 代码库 > java Socket编程-基于TCP
java Socket编程-基于TCP
package com.wzy.Test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;/** * * @author wzy *服务器线程处理类 */public class ServerThread extends Thread { //和本线程相关的Socket Socket socket; public ServerThread(Socket socket){ this.socket = socket; } public void run(){ PrintWriter pw=null; OutputStream out=null; BufferedReader br=null ; InputStreamReader isr=null; InputStream is=null; try{ is = socket.getInputStream();//字节输入流 //将字节流转换为字符流 isr = new InputStreamReader(is); //为输入流添加缓冲 br = new BufferedReader(isr); String info = ""; String info1 = null; while((info = br.readLine())!=null){ //循环读取客户端信息 System.out.println("服务器接收到客户端的信息为: "+info); info1 = info; } System.out.println(info1); //关闭输入流 socket.shutdownInput(); //4获取输出流,响应客户端的请求 out = socket.getOutputStream(); pw = new PrintWriter(out); pw.write("你好,客户端,我是服务器!"); pw.flush(); //5.关闭资源 } catch(Exception e){ e.printStackTrace(); } finally{ try{ if(pw!=null){pw.close();} if(out!=null){out.close();} if(br!=null){br.close();} if(isr!=null){isr.close();} if(is!=null){is.close();} if(socket!=null){socket.close();} } catch(IOException e){ e.printStackTrace(); } } }}
package com.wzy.Test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;/** * * @author wzy *服务器 */public class Server { public static void main(String[] args) { int count = 0; //for(int a = 0;a<5;a++){ try { //1.创建一个服务器端socket,即ServerSocket,指定绑定的端口,并监听 ServerSocket serverSocket = new ServerSocket(8888);//监听8888端口 System.out.println("服务器等待客户端连接...."); //2.调用accept开始监听,等待客户端连接 Socket socket =null; while(true){ socket = serverSocket.accept();//接受 ServerThread serverThread = new ServerThread(socket); serverThread.start(); System.out.println("客户连接数量:"+(++count)); InetAddress address = socket.getInetAddress(); System.out.println("客户端ip:"+address.getHostAddress()); }/* //3.获取输入流,并读取客户端信息 InputStream is = socket.getInputStream();//字节输入流 //将字节流转换为字符流 InputStreamReader isr = new InputStreamReader(is); //为输入流添加缓冲 BufferedReader br = new BufferedReader(isr); String info = ""; String info1 = null; while((info = br.readLine())!=null){ //循环读取客户端信息 System.out.println("服务器接收到客户端的信息为: "+info); info1 = info; } System.out.println(info1); //关闭输入流 socket.shutdownInput(); //4获取输出流,响应客户端的请求 OutputStream out = socket.getOutputStream(); PrintWriter pw = new PrintWriter(out); pw.write("你好,客户端,我是服务器!"); pw.flush(); //5.关闭资源 pw.close(); out.close(); br.close(); isr.close(); is.close(); socket.close(); serverSocket.close();*/ } catch (Exception e) { e.printStackTrace(); } }//}}
package com.wzy.Test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;/** * * @author wzy *客户端 */public class Client { public static void main(String[] args) { try { //1.创建客户端socket,指定服务器地址和端口 Socket socket = new Socket("127.0.0.1", 8888); //2.获取输出流,向服务器发送信息 OutputStream os = socket.getOutputStream();//字节输出流 PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流 pw.write("hello,服务器!!!,我是客户端"); pw.flush();//刷新缓存 socket.shutdownOutput();//关闭输出流 //3.获取输入流,读取服务器的响应 InputStream in = socket.getInputStream(); InputStreamReader inr = new InputStreamReader(in); BufferedReader brr = new BufferedReader(inr); String info = null; while((info =brr.readLine())!=null){ System.out.println("客户端接到服务器的消息: "+info); } brr.close(); inr.close(); in.close(); //4.关闭资源 pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
java Socket编程-基于TCP
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。