我写了个GUI的服务器端和客户端,服务器继承Thread类,每次new 一个线程出来,问题是每次对话时都时临时建立socket,inputstream,outputstream,这样会不会浪费资源,能不能从头到尾,就用一开始建立的一个socket,inputstream,outputstream,直到关闭程序再调用 close()?如果行的话,请高手务必给出代码,感激不尽!
解决方案 »
- 写了一个java QQ 聊天系统 只能基于局域网聊天通信,如何实现Internet上面的聊天通信?
- 出现一个异常,大家帮忙看一下
- swing 定制鼠标光标问题
- 类反射(字段无序)
- java程序开机自动运行
- 30分,简单问题:有关于如何用java显示一个图象
- 菜鸟问个关于swing GUI中paint();的问题
- 请教高手学习java什么比较好!!!!!!!!!!!!!
- 询问一个对于我来说非常重要的问题,回答者均有分
- 不知道有没有人会啊~~~~~~~~~~~~~~~~~~~~~~~~~~全部的分了~~谢谢啊~!
- 求助!我在applet类里实例化一个类并启动另外一个线程运行,实例化的这个类调用一个配置文件subxcribe.config,运行是提示java.io.FileNotFo
- 因为点的数值太小,几乎看不到画出来的线条。。。
import java.net.*;
import java.io.*;
import java.util.*;
public class socketserver implements Runnable{
private Socket client=null;
private BufferedReader netin=null;
private PrintWriter out=null;
public socketserver(Socket s){
client=s;
try{
netin=new BufferedReader(new InputStreamReader(client.getInputStream()));
out=new PrintWriter(client.getOutputStream());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
System.out.println("socket server is listening....");
ServerSocket server=null;
try
{
server=new ServerSocket(65530);
while(true)
{
Socket user=server.accept();
new Thread(new socketserver(user)).start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
server.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void run()
{
try
{
// System.out.println(Thread.currentThread().getName()+"working....");
while(true)
{
String tmpstr=netin.readLine();
System.out.println(client.getInetAddress()+"@"+client.getPort()+" Says:"+tmpstr);
if(tmpstr.equalsIgnoreCase("bye"))
{
out.close();
netin.close();
client.close();
return;
}
else out.println(tmpstr);
}
}
catch(Exception e)
{
e.printStackTrace();
try{out.close();
netin.close();
client.close();}
catch(Exception ee)
{
}
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class socketclient extends JFrame implements ActionListener
{
TextArea sysInfo=new TextArea(" ");
Label lbl_txt=new Label("TEXT:");
TextField txt_text =new TextField(30);
JButton btn_send=new JButton("send");
JScrollPane jp=new JScrollPane(sysInfo);
JPanel p=new JPanel();
boolean isConn=false;
Socket client = null;
InputStream netin=null;
PrintWriter out=null;
public socketclient()
{
setSize(430,320);
btn_send.addActionListener(this);
p.setLayout(new FlowLayout(FlowLayout.LEFT));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
setTitle("正在退出...");
System.exit(0);
}
});
getContentPane().add(p,"North");
getContentPane().add(jp,"Center");
p.add(lbl_txt);
p.add(txt_text);
p.add(btn_send);
show();
connect();
}
public void actionPerformed(ActionEvent e)
{
Object eventsource=e.getSource();
if(eventsource==btn_send)
{
send();
}
}
public static void main(String[] args)
{
new socketclient();
}
public void connect()
{
try{
client = new Socket("localhost",65530);
netin=client.getInputStream();
out=new PrintWriter(client.getOutputStream(),true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void send()
{
try
{
out.println(txt_text.getText());
sysInfo.append("\r\n我说:"+txt_text.getText());
byte[] buf=new byte[100];
int len=netin.read(buf);
sysInfo.append("\r\nServer:"+new String(buf,0,len));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void closeconn()
{
try{
out.close();
netin.close();
client.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}