帮忙看下 java socket 跟 jframe

new 个 JFrame 再new Socket 了来就卡死了。请帮忙看看是怎么回事,刚学java贴出来

 

import java.awt.BorderLayout;


//读取信息线程
class GetMessage extends Thread
{
	private BufferedReader br=null;
	private Boolean flag=true;

	public GetMessage(BufferedReader br)
	{
		this.br=br;
	}
	@Override
	public void run() {
		while(flag)
		{
			try {
				String str=br.readLine();
				if(str.isEmpty())
				{
					flag=false;
					br.close();
				}
				System.out.println("系统返回来的: "+str);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		
	}
}

class SendMessage extends Thread
{
	private BufferedReader br=null;
	private PrintWriter pw=null;
	private Boolean flag=true;

	public SendMessage(PrintWriter pw,BufferedReader br)
	{
		this.br=br;
		this.pw=pw;
	}
	@Override
	public void run() {
		while(flag)
		{	
			SocketClient.OutStreamStr(pw, br);	
		}		
	}
}

//主进程
public class SocketClient {
	private Socket client=null;
	private Boolean flag=true;
	private BufferedReader br =null;
	private BufferedReader br2 =null;
	private PrintWriter pw = null;
	public SocketClient()
	{
		try {

			client=new Socket();
			client.connect(new InetSocketAddress("127.0.0.1",2000));
			pw = new PrintWriter(client.getOutputStream(),true);		
			br2=new BufferedReader(new InputStreamReader(System.in));
		
			br =new BufferedReader(new InputStreamReader(client.getInputStream())); //从服务器读来的信息
			GetMessage gm = new GetMessage(br);
			gm.start();
			SendMessage sm = new SendMessage(pw,br2);
			sm.start();
/*			while(flag)
			{	
				sendMessage(br2.readLine());
				//pw.println(br2.readLine());
				//pw.flush();
				//System.out.println("系统返回来的: "+br.readLine());
			}*/
			try {
				closeSocket();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void OutStreamStr(PrintWriter pw,BufferedReader br)
	{
		try {
			pw.println(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void sendMessage(String str)
	{
		pw.println(str);
	}
	//关闭
	private void closeSocket() throws Exception
	{	
		flag=false;
		if(br!=null)
			br.close();
		if(br2!=null)
			br2.close();
		if(pw!=null)
			pw.close();
		if(!client.isClosed())
			client.close();
	}	
	
}

 import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

//GUI
//borderLayout把一个容器分为五块东西南北中,flowLayout是流型的,一行排不完下行继续。gridlayout是网格型的,各个格子大小都一样。
class ClientChat extends JFrame {
	public ClientChat()
	{
		new SocketClient();
		//发送按钮布局
		JPanel jp1 = new JPanel(new BorderLayout());
		JButton jb=new JButton("发送");
		JTextField jtf=new JTextField("label:");
		jp1.add(jtf,BorderLayout.WEST);
		jp1.add(jb,BorderLayout.SOUTH);
		
		
		JScrollPane jsp= new JScrollPane();
		JTextArea ta= new JTextArea("", 5, 50);;
		ta.setLineWrap(true);
		//jsp.setSize(400, 250);
		jsp.add(ta);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		this.add(ta,"Center");
		this.add(jp1,BorderLayout.SOUTH);
		this.setSize(400, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {	
		new ClientChat();
	}
	//@Override
	public void run() {
			
	}

}

 import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;


public class SocketServer {

	private ServerSocket server=null;
	private Boolean flag=true;
	private BufferedReader br=null;
	private PrintWriter pw=null;
	
	public SocketServer()
	{
		try {
			server = new ServerSocket(2000);
			while(flag)
			{
				Socket s=server.accept();
				new ListSocket(s).start();
			}
			server.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new SocketServer();
	}

	class ListSocket extends Thread
	{
		private Socket s=null;
		private Boolean flag1=true;
		private BufferedReader br =null;
		private PrintWriter pw = null;
		public ListSocket(Socket s)
		{
			this.s=s;
			//ListSaveSocket.listSocket.put("1", s);		
			//Socket hm=ListSaveSocket.listSocket.get("1");
		}
		private void closeSocket() throws Exception
		{	
			flag1=false;
			if(br!=null)
				br.close();
			if(pw!=null)
				pw.close();
			if(!s.isClosed())
				s.close();
			System.out.println("半闭了连接");
		}
		@Override
		public void run() {
			try {				
				System.out.println("有哥们进来了");
				br = new BufferedReader(new InputStreamReader(s.getInputStream()));
				pw = new PrintWriter(s.getOutputStream());
				while(flag1)
				{				
					String str= br.readLine();
					if(str.equals(""))
					{
						//flag=false;
						//s.close();
					}
					//pw.println(str);
					//pw.flush();
					System.out.println("哥们发过来的信息: "+str);
				}
				try {
					closeSocket();
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (IOException e) {				
				try {
					closeSocket();
						
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				System.out.println(s.isConnected());
				System.out.println(s.isInputShutdown());
				System.out.println(s.isOutputShutdown());
				e.printStackTrace();
			}

		}
	}
}
class ListSaveSocket
{
	public static HashMap<String, Socket> listSocket =null;
}
找到问题了,用了个system.in控制台来输入,又new了个窗口出来就冲突了吧

                            

你可能感兴趣的:(java socket)