77范文网 - 专业文章范例文档资料分享平台

java2实用教程第4版答案(7)

来源:网络收集 时间:2018-12-29 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

{ Socket socket;

DataOutputStream out=null; DataInputStream in=null; String s=null; boolean quesion=false; Server_thread(Socket t) { socket=t;

try { out=new DataOutputStream(socket.getOutputStream()); in=new DataInputStream(socket.getInputStream()); }

catch (IOException e) {} }

public void run() { while(true)

{ double a[]=new double[3] ; int i=0;

try{ s=in.readUTF();//堵塞状态,除非读取到信息 quesion=false;

StringTokenizer fenxi=new StringTokenizer(s,\ while(fenxi.hasMoreTokens()) { String temp=fenxi.nextToken();

try{ a[i]=Double.valueOf(temp).doubleValue();i++; }

catch(NumberFormatException e) { out.writeUTF(\请输入数字字符\ quesion=true; } }

if(quesion==false)

{ double p=(a[0]+a[1]+a[2])/2.0;

out.writeUTF(\ } }

catch (IOException e)

{ System.out.println(\客户离开\ return; } } } }

2. 客户端Client.java

import java.net.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Client

{ public static void main(String args[]) { new ChatClient(); } }

class ChatClient extends Frame implements Runnable,ActionListener { Button connection,send;

TextField inputName,inputContent; TextArea chatResult; Socket socket=null; DataInputStream in=null; DataOutputStream out=null; Thread thread; String name=\ public ChatClient () { socket=new Socket();

Box box1=Box.createHorizontalBox(); connection=new Button(\连接服务器\ send=new Button(\发送\ send.setEnabled(false); inputName=new TextField(6); inputContent=new TextField(22); chatResult=new TextArea(); box1.add(new Label(\输入妮称:\ box1.add(inputName); box1.add(connection);

Box box2=Box.createHorizontalBox(); box2.add(new Label(\输入聊天内容:\ box2.add(inputContent); box2.add(send);

connection.addActionListener(this); send.addActionListener(this); thread=new Thread(this); add(box1,BorderLayout.NORTH);

add(box2,BorderLayout.SOUTH); add(chatResult,BorderLayout.CENTER); setBounds(10,30,400,280); setVisible(true); validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0); } }); }

public void actionPerformed(ActionEvent e) { if(e.getSource()==connection) { try

{ if(socket.isConnected()) {} else

{ InetAddress address=InetAddress.getByName(\

InetSocketAddress socketAddress=new InetSocketAddress(address,666); socket.connect(socketAddress);

in =new DataInputStream(socket.getInputStream()); out = new DataOutputStream(socket.getOutputStream()); name=inputName.getText(); out.writeUTF(\姓名:\ send.setEnabled(true); if(!(thread.isAlive())) thread=new Thread(this); thread.start(); } }

catch (IOException ee){} }

if(e.getSource()==send)

{ String s=inputContent.getText(); if(s!=null)

{ try { out.writeUTF(\聊天内容:\ }

catch(IOException e1){} }

} }

public void run() { String s=null; while(true)

{ try{ s=in.readUTF();

chatResult.append(\ }

catch(IOException e)

{ chatResult.setText(\与服务器已断开\ try { socket.close(); }

catch(Exception exp) {} break; } } } }

服务器端ChatServer.java

import java.io.*; import java.net.*; import java.util.*; public class ChatServer

{ public static void main(String args[]) { ServerSocket server=null; Socket you=null;

Hashtable peopleList; peopleList=new Hashtable(); while(true)

{ try { server=new ServerSocket(666); }

catch(IOException e1)

{ System.out.println(\正在监听\ }

try { you=server.accept(); InetAddress address=you.getInetAddress(); System.out.println(\客户的IP:\ }

catch (IOException e) {} if(you!=null)

{ Server_thread peopleThread=new Server_thread(you,peopleList); peopleThread.start(); }

else { continue; } } } }

class Server_thread extends Thread { String name=null; Socket socket=null; File file=null;

DataOutputStream out=null; DataInputStream in=null; Hashtable peopleList=null;

Server_thread(Socket t,Hashtable list) { peopleList=list; socket=t;

try { in=new DataInputStream(socket.getInputStream()); out=new DataOutputStream(socket.getOutputStream()); }

catch (IOException e) {} }

public void run() { while(true) { String s=null; try{

s=in.readUTF(); if(s.startsWith(\姓名:\ { name=s;

boolean boo=peopleList.containsKey(name); if(boo==false)

{ peopleList.put(name,this); } else

{ out.writeUTF(\请换妮称:\ socket.close();

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java2实用教程第4版答案(7)在线全文阅读。

java2实用教程第4版答案(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/396732.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: