當前位置:編程學習大全網 - 編程語言 - 求java nio網絡編程的小例子,要求客戶端壹直與服務器保持連接

求java nio網絡編程的小例子,要求客戶端壹直與服務器保持連接

import java.awt.*;

import java.awt.event.*;

import java.io.IOException;

import java.net.*;

import java.io.*;

public class chatClient extends Frame {

/**

* @param args

*/

TextField tfTxT=new TextField();

TextArea taContent=new TextArea();

Socket s=null;

DataOutputStream dos=null;

DataInputStream dis=null;

private boolean bConnected =false;

public static void main(String[] args) {

new chatClient().lunachFrame();

}

private class RecvThread implements Runnable{

public void run() {

try{

while(bConnected){

String str=dis.readUTF();

taContent.setText(taContent.getText()+str+'\n');

}

}catch(IOException e){

e.printStackTrace();

}

}

}

public void lunachFrame(){

this.setLocation(400, 300);

this.setSize(300,300);

//this.setLayout(new FlowLayout());

this.add(tfTxT,"South");

this.add(taContent,"North");

pack();

tfTxT.addActionListener(new TFListener());

this.addWindowListener(new WindowClose());

this.setVisible(true);

connect();

new Thread(new RecvThread()).start();

}

public void connect(){

try {

s= new Socket("127.0.0.1",8888);

dos =new DataOutputStream(s.getOutputStream());

dis =new DataInputStream(s.getInputStream());

System.out.println("connected!");

bConnected=true;

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public void disconnect(){

try {

dos.close();

s.close();

} catch (Exception e) {

// TODO 自動生成 catch 塊

e.printStackTrace();

}

}

class WindowClose extends WindowAdapter{

@Override

public void windowClosing(WindowEvent e) {

// TODO 自動生成方法存根

System.exit(0);

disconnect();

}

}

private class TFListener implements ActionListener{

public void actionPerformed(ActionEvent e) {

String str=tfTxT.getText().trim();//trim去掉兩邊空格

//taContent.setText(str);

tfTxT.setText("");

try {

dos.writeUTF(str);

dos.flush();

//dos.close();

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

}

======================================

import java.io.IOException;

import java.net.*;

import java.io.*;

import java.util.*;

public class ChatServer {

List<Client> clients=new ArrayList<Client>();

Client c=null;

public static void main(String[] args){

new ChatServer().start();

}

public void start(){

boolean started=false;

ServerSocket ss=null;

DataInputStream dis=null;

try{

ss=new ServerSocket(8888);

started =true;

}catch(Exception e)

{

e.printStackTrace();

}

try{

while(started){

Socket s=ss.accept();

c=new Client(s);//啟動線程,實行run()方法

System.out.println("a client connected!");

new Thread(c).start();//啟動start方法,循環.start是Thread中的方法與這上面的start無關

clients.add(c);

//dis.close();

}

} catch (Exception e) {

//e.printStackTrace();

}

finally{

try {

ss.close();

} catch (IOException e) {

// TODO 自動生成 catch 塊

e.printStackTrace();

}

}

}

class Client implements Runnable{

private Socket s;

private DataInputStream dis =null;

private boolean bConnected =false;

private DataOutputStream dos=null;

public Client(Socket s){

this.s=s;

try {

dis=new DataInputStream(s.getInputStream());

dos =new DataOutputStream(s.getOutputStream());

bConnected =true;

} catch (IOException e) {

// TODO 自動生成 catch 塊

e.printStackTrace();

}

}

public void send(String str)throws Exception{

dos.writeUTF(str);

}

public void run() {

try{

while(bConnected){

String str = dis.readUTF();

System.out.println(str);

for(int i=0;i<clients.size();i++){

c=clients.get(i);

c.send(str);

}

/*for(Iterator<Client> it=clients.iterator();it.hasNext();){

Client c=it.next();

c.send(str);

}*/

}

}catch(SocketException e){

clients.remove(this);

System.out.println("客戶下線了");

}

catch(EOFException e){

System.out.println("Client closed");

}

catch (Exception e){

//e.printStackTrace();

}

finally{

try {

if(dis !=null) dis.close();

if(dos !=null) dos.close();

if(s!=null) s.close();

} catch (Exception e1) {

// TODO 自動生成 catch 塊

//e1.printStackTrace();

}

}

}

}

}

第壹個是客戶端,

第二個是server端

  • 上一篇:員工對未來工作的規劃範文
  • 下一篇:網上有哪些免費的教育資源嗎?哪個比較實用?
  • copyright 2024編程學習大全網