[JAVA100例]051、数据报通讯:聊天吧[Java编程]
本文“[JAVA100例]051、数据报通讯:聊天吧[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
//文件名:ChatClient.java
import java.net.*;
import java.io.*;
/**
* <p>Title: 网络聊天吧</p>
* <p>Description: 这是一个利用数据报通讯方法的聊天程序的客户端</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ChatClient.java</p>
* @version 1.0
*/
public class ChatClient{
private DatagramSocket s;
private InetAddress hostAddress;
private byte[] buf = new byte[1000];
private DatagramPacket dp = new DatagramPacket(buf,buf.length);
/**
*<br>办法阐明:构造器,这里实现接纳用户输入和与服务器通讯
*<br>输入参数:
*<br>返回范例:
*/
public ChatClient(){
try{
//利用构造器,成立利用本机任何可用端口的数据包Socket
s = new DatagramSocket();
//获得本地IP
hostAddress = InetAddress.getByName("localhost");
System.out.println("Client start............");
while(true){
String outMessage ="";
//读取输入
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try{
outMessage = stdin.readLine();
}catch(IOException ie){
System.err.println("IO error!");
}
//假如输入“bye”则表示退出程序
if(outMessage.equals("bye")) break;
String outString = "Client say: "+ outMessage;
byte[] buf = outString.getBytes();
//打包数据,发送数据
DatagramPacket out = new DatagramPacket(buf,buf.length,hostAddress,ChatServer.PORT);
s.send(out);
//等候服务器返回
s.receive(dp);
String rcvd = "rcvd from "+ dp.getAddress() + ", " + dp.getPort() +
": "+ new String(dp.getData(),0,dp.getLength());
System.out.println(rcvd);
}
}catch(UnknownHostException e){
System.out.println("Can;t open socket");
System.exit(1);
}catch(SocketException e){
System.out.println("Can;t open socket");
e.printStackTrace();
System.exit(1);
}catch(IOException e){
System.err.println("Communication error");
e.printStackTrace();
System.exit(1);
}catch(Exception e){
System.err.println("Communication error");
e.printStackTrace();
System.exit(1);
}
System.out.println("ChatClient over");
}
/**
*<br>办法阐明:主办法
*<br>输入参数:
*<br>返回范例:
*/
public static void main(String[] args){
new ChatClient();
}
}
import java.net.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: 网络聊天吧</p>
* <p>Description: 利用数据报成立的聊天服务器</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ChatServer.java</p>
* @version 1.0
*/
public class ChatServer{
static final int PORT = 4000;//设置服务端口
private byte[] buf = new byte[1000];
private DatagramPacket dgp =new DatagramPacket(buf,buf.length);
private DatagramSocket sk;
/**
*<br>办法阐明:服务端构造器,实现读取用户输入和通讯
*<br>输入参数:
*<br>返回范例:
*/
public ChatServer(){
try{
//实例化数据报
sk = new DatagramSocket(PORT);
System.out.println("Server start.................");
while(true){
//等候接纳
sk.receive(dgp);
//获得接纳信息
String rcvd = new String(dgp.getData(),0,dgp.getLength())+
", from address: "+ dgp.getAddress()+
", port: "+ dgp.getPort();
System.out.println(rcvd);
String outMessage ="";
//读取输入
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try{
outMessage = stdin.readLine();
}catch(IOException ie){
System.err.println("IO error!");
}
String outString = "Server say: "+ outMessage;
//拷贝字符到缓存
byte[] buf = outString.getBytes();
//打包数据,发送复书息.
DatagramPacket out = new DatagramPacket(buf,buf.length,dgp.getAddress(),dgp.getPort());
sk.send(out);
}
}catch(SocketException e){
System.err.println("Can´t open socket");
System.exit(1);
}catch(IOException e){
System.err.println("Communication error");
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args){
new ChatServer();
}
}
以上是“[JAVA100例]051、数据报通讯:聊天吧[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |