clientthread.java

来自「java编写的sip协议软电话,结构非常清晰的SIP协议栈」· Java 代码 · 共 63 行

JAVA
63
字号
package JavaPhoneTeam10;

import java.io.*;
import java.net.*;

class ClientThread extends Thread {
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	private static int threadcount = 0;
	
	public ClientThread(InetAddress addr) {
		
		try {
			socket = new Socket(addr, PhoneServer.PORT);
		} 
		catch (IOException e) {
			System.err.println("Socket failed");
		}//end of catch
		
		try {
			in = new BufferedReader(
				   new InputStreamReader(
				   	 socket.getInputStream()));
			out = new PrintWriter(
				    new BufferedWriter(
				      new OutputStreamWriter(
				      	socket.getOutputStream())),true);
			start();
		} 
		catch(IOException e) {
			try {
				socket.close();
			} 
			catch(IOException e2) {
				System.err.println("Socket not closed");
			}
		}//end of catch(IOException e)
		//otherwise the socket will be closed by the run() method of the thread.
	}//end of JabberClientThread(InetAddress addr)
	
	public void run(){
		try {
			String str = in.readLine();
			System.out.println(str);
			out.println("END");
		} 
		catch(IOException e ) {
			System.err.println("Socket not closed");
		}
	}
}

public class ClientThread {
	public static void main(String[] args)
    throws IOException, InterruptedException {
	  	InetAddress addr = InetAddress.getByName(null);
	    while(true) {
	   	    new ClientThread(addr);
    	  	Thread.currentThread().sleep(1000);
	    }//end of while
	 }//end of main()
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?