標籤:java 多線程synchronized
1、socket服務端
package com.socket.project;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class Service {public static void main(String[] args) {Socket socket=null;InputStream is=null;InputStreamReader isread=null;BufferedReader red=null;try {//1.建立一個伺服器端Socket,即ServerSocket,指定綁定的連接埠,並監聽此連接埠ServerSocket server=new ServerSocket(8089);System.out.println("伺服器啟動監聽。。。。。。");//監聽連接埠while(true){socket=server.accept();//擷取輸入資料流 is=socket.getInputStream(); isread=new InputStreamReader(is);//建立緩衝區 red=new BufferedReader(isread);String data=null;while((data=red.readLine())!=null){System.out.println("用戶端上傳的資料是:"+data);}} } catch (IOException e) {e.printStackTrace();}finally{if(red!=null)try {red.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(isread!=null)try {isread.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(is!=null)try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}2、用戶端
package com.socket.project;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class SocketClient {//socket 用戶端public static void main(String[] args) {OutputStream out=null;PrintWriter writer=null;try {//建立用戶端socket 傳遞的 地址 連接埠Socket sock=new Socket("localhost", 8089); out=sock.getOutputStream();//位元組流 writer=new PrintWriter(out);//將位元組流轉換列印流 writer.print("我想告訴你,我一直在這裡等著你"); writer.flush(); //關閉socket輸出資料流 sock.shutdownOutput();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//關閉流if(writer!=null)writer.close();if(out!=null)try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
多線程編程
package com.socket.thread;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;public class SocketClient {//socket 用戶端public static void main(String[] args) {OutputStream out=null;PrintWriter writer=null;InputStream input=null;InputStreamReader read=null;BufferedReader red=null;Socket sock=null;try {//建立用戶端socket 傳遞的 地址 連接埠 sock=new Socket("localhost", 8089); out=sock.getOutputStream();//位元組流 writer=new PrintWriter(out);//將位元組流轉換列印流 writer.print("我想告訴你,我一直在這裡等著你"); writer.flush(); //關閉socket輸出資料流 sock.shutdownOutput(); //擷取服務端返回的資訊 input =sock.getInputStream();//擷取輸入資料流 read=new InputStreamReader(input); red=new BufferedReader(read); String data=""; while((data=red.readLine())!=null){System.out.println("服務端回複你:"+data); }} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//關閉流if(writer!=null)writer.close();if(out!=null)try {out.close();} catch (IOException e) {e.printStackTrace();}if(red!=null)try {red.close();} catch (IOException e) {e.printStackTrace();}if(read!=null)try {read.close();} catch (IOException e) {e.printStackTrace();}if(input!=null)try {input.close();} catch (IOException e) {e.printStackTrace();}if(sock!=null)try {sock.close();} catch (IOException e) {e.printStackTrace();}}}}
package com.socket.thread;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class Service {public static void main(String[] args) {//1.建立一個伺服器端Socket,即ServerSocket,指定綁定的連接埠,並監聽此連接埠try {ServerSocket serverSocket=new ServerSocket(8089);Socket socket=null;while(true){//一直監聽socket=serverSocket.accept();//啟動一個服務SocketServiceThread thread=new SocketServiceThread(socket);thread.start();}} catch (IOException e) {e.printStackTrace();}}}
package com.socket.thread;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;public class SocketServiceThread extends Thread {private Socket socket=null;public SocketServiceThread(Socket socket){this.socket=socket;}//執行線程操作@Overridepublic void run() {InputStream input=null;InputStreamReader red=null;BufferedReader br=null;OutputStream out=null;PrintWriter writer=null;try {//擷取輸入資料流input=socket.getInputStream();red=new InputStreamReader(input);//緩衝字元br=new BufferedReader(red);String data="";while((data=br.readLine())!=null){System.out.println("用戶端來信說:"+data);}//關閉寫入流socket.shutdownInput();//擷取輸出資料流out=socket.getOutputStream();// 擷取輸出位元組流writer=new PrintWriter(out);//將位元組轉換列印流writer.write("歡迎光臨!!");writer.flush();} catch (IOException e) {e.printStackTrace();}finally{if(br!=null)try {br.close();} catch (IOException e) {e.printStackTrace();}if(red!=null)try {red.close();} catch (IOException e) {e.printStackTrace();}if(input!=null)try {input.close();} catch (IOException e) {e.printStackTrace();}if(writer!=null)writer.close();if(out!=null)try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
java中的socket編程