大家好,今天小編來為大家解答extends thread這個問題,如何實現socket的長連接很多人還不知道,現在讓我們一起來看看吧!
如何實現socket的長連接
現編這個就是個多線程服務器,只要在client不釋放連接,服務器端的run里邊寫while(TRUE)循環,那么就可以長期連接。classConnectionThreadextendsThread{Socketclient;intcounter;publicConnectionThread(Socketcl,intc){client=cl;counter=c;}@Overridepublicvoidrun(){try{StringdestIP=client.getInetAddress().toString()
;intdestport=client.getPort()
;PrintStreamoutstream=newPrintStream(client.getOutputStream())
;DataInputStreaminstream=newDataInputStream(client.getInputStream())
;Stringinline=instream.readLine();}//trycatch(IOExceptione){System.out.println(e);}}//run
如何eclipse編寫一個簡單實用的登陸界面
//服務器端代碼
importjava.awt.FlowLayout;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Iterator;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
publicclassQLServerextendsJFrame{
publicJTextAreajtextarea=null;
publicvoidlanuchFrame(Stringstr){
this.setName(str);
init();
}
privatevoidinit(){
setLayout(newFlowLayout());
jtextarea=newJTextArea(20,17);
jtextarea.setLineWrap(true);
jtextarea.setEditable(false);
this.getContentPane().add(newJScrollPane(jtextarea));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,400);
setLocationRelativeTo(null);
setResizable(false);
}
ServerSocketserver=null;
CollectioncClients=newArrayList<ClientConn>();//加個泛型
publicvoidstartServer()throwsIOException{
while(true){
Sockets=server.accept();
cClients.add(newClientConn(s));
jtextarea.append("newclientlogin"+s.getInetAddress()+":"+s.getPort()+"\n");
}
}
publicQLServer(intport,Stringstr)throwsIOException{
server=newServerSocket(port);
lanuchFrame(str);
}
classClientConnimplementsRunnable
{
Sockets=null;
publicClientConn(Sockets)
{
this.s=s;
(newThread(this)).start();
}
publicvoidsend(Stringstr)throwsIOException
{
DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
publicvoiddispose()//客戶端下線
{
try{
if(s!=null)s.close();
cClients.remove(this);
jtextarea.append("Aclientout!\n");
jtextarea.append("clientcount:"+cClients.size()+"\n\n");
}
catch(Exceptione)
{
e.printStackTrace();
}
}
publicvoidrun()
{
try{
DataInputStreamdis=newDataInputStream(s.getInputStream());
Stringstr=dis.readUTF();
while(str!=null&&str.length()!=0)
{
System.out.println(str);
for(Iteratorit=cClients.iterator();it.hasNext();)
{
ClientConncc=(ClientConn)it.next();
if(this!=cc)
{
cc.send(str+""+s.getInetAddress().getHostName());
}
}
str=dis.readUTF();//少了這句話會無限輸出
//send(str);
}
this.dispose();
}
catch(Exceptione)
{
this.dispose();
}
}
}
publicstaticvoidmain(String[]args){
try{
QLServerqlserver=newQLServer(8888,"QLServer");
qlserver.startServer();
}catch(IOExceptione){
e.printStackTrace();
}
}
}importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.BufferedReader;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.InetAddress;
importjava.net.Socket;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
//客戶端代碼
publicclassQLClientextendsJFrameimplementsActionListener{
publicJTextAreajtextarea1=null;
publicJTextAreajtextarea2=null;
publicJButtonbutton=null;
Sockets=null;
publicvoidlaunchFrame(Stringstr){
this.setName(str);
init();
}
publicQLClient(Stringstr)throwsIOException{
launchFrame(str);
s=newSocket("127.0.0.1",8888);//哪臺電腦做服務器,IP地址改成那臺機子的IP
(newThread(newServeConn())).start();
}
privatevoidinit(){
setLayout(newFlowLayout());
jtextarea1=newJTextArea(17,16);
jtextarea2=newJTextArea(4,16);
jtextarea1.setLineWrap(true);
jtextarea1.setEditable(false);
jtextarea2.setLineWrap(true);
button=newJButton("發送");
button.addActionListener(this);//綁定button事件
this.getContentPane().add(newJScrollPane(jtextarea1));
this.getContentPane().add(newJScrollPane(jtextarea2));
add(button);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,470);
setLocationRelativeTo(null);
setResizable(false);
}
publicvoidsend(Stringstr)throwsIOException{
DataOutputStreamdos=newDataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==button){
StringsendStr=jtextarea2.getText();
if(sendStr.trim().length()==0){
return;
}
try{
this.send(sendStr);
jtextarea2.setText("");
InetAddressa;
a=InetAddress.getLocalHost();
Stringhostname=a.getHostName();
jtextarea1.append(sendStr+"("+hostname+")"+"\n");
}catch(IOExceptione1){
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
}
}
classServeConnimplementsRunnable{
publicvoidrun(){
if(s==null)return;
try{
DataInputStreamdis=newDataInputStream(s.getInputStream());
Stringstr=dis.readUTF();
while(str!=null&&str.length()!=0)
{
//System.out.println(str);
QLClient.this.jtextarea1.append(str+"\n");//內部類用外類中的變量或方法加外類名
str=dis.readUTF();
}
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
//main主函數入口
publicstaticvoidmain(String[]args)throwsIOException{
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
QLClientqlclient=newQLClient("QLClient");
Stringstr=br.readLine();
while(str!=null&&str.length()!=0){
qlclient.send(str);
str=br.readLine();//防止死循環
}
qlclient.s.close();
}
}
好了,本文到此結束,如果可以幫助到大家,還望關注本站哦!