java 两人聊天小程序
importjava.awt.*;importjava.io.*;importjava.net.*;importjava.awt.event.*;publicclassC...
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class ChatServer{
public static void main(String argus[]){
new MyFrames().initnet();
}
}
class MyFrames extends Frame{
private String s1,s2;
private ServerSocket server;
private Socket ss;
private PrintWriter ps ;
private BufferedReader bf;
private Panel p1;
private TextArea tx1, tx2;
private Button b1, b2;
public MyFrames(){
super("Server");
setLocation(200,200);
setSize(500,600);
tx1 = new TextArea();
tx2 = new TextArea();
tx1.setEditable(false);
p1 = new Panel();
b1 = new Button("send");
b2 = new Button("cancel");
b1.addActionListener(new ActionChat());
b2.addActionListener(new ActionChat());
p1.add(b1);
p1.add(b2);
add(tx1,BorderLayout.NORTH);
add(tx2,BorderLayout.CENTER);
add(p1,BorderLayout.SOUTH);
tx1.setSize(500,400);
tx2.setSize(500,150);
setVisible(true);
}
public void initnet(){
try{
server = new ServerSocket(6666);
ss = server.accept();
System.out.print("success");
ps = new PrintWriter(ss.getOutputStream());
bf = new BufferedReader(new InputStreamReader(ss.getInputStream()));
s2=bf.readLine();
System.out.print(s2);
tx1.setText("client:"+s2+"\n");
}
catch(IOException e){}
}
class ActionChat implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("cancel")){
try{
ss.close();
ps.close();
bf.close();
}
catch(IOException e2){}
System.exit(-1);
}
else{
//
s1 = tx2.getText();
ps.println(s1);
ps.flush();
tx1.append("Server:"+s1+"\n");
tx2.setText("");
tx2.setEditable(false);
// }
// catch(IOException e1){}
}
}
}
}
//这是服务器端 先是客户端说 然后服务器读 再是服务器说 客户端读
但是出问题了 就是只能实现一次读呀 帮改改 再给说下怎么能实现可以随便一个人可以读写 不用的待对方 (是现在这个给加分)
服务器BufferedReader br 为什么要是成员变量呢 ,不是服务器端的每个socket对象都有一个流么?这样不是所有的socket对象用的是一个流呀(对于3楼的程序) 展开
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class ChatServer{
public static void main(String argus[]){
new MyFrames().initnet();
}
}
class MyFrames extends Frame{
private String s1,s2;
private ServerSocket server;
private Socket ss;
private PrintWriter ps ;
private BufferedReader bf;
private Panel p1;
private TextArea tx1, tx2;
private Button b1, b2;
public MyFrames(){
super("Server");
setLocation(200,200);
setSize(500,600);
tx1 = new TextArea();
tx2 = new TextArea();
tx1.setEditable(false);
p1 = new Panel();
b1 = new Button("send");
b2 = new Button("cancel");
b1.addActionListener(new ActionChat());
b2.addActionListener(new ActionChat());
p1.add(b1);
p1.add(b2);
add(tx1,BorderLayout.NORTH);
add(tx2,BorderLayout.CENTER);
add(p1,BorderLayout.SOUTH);
tx1.setSize(500,400);
tx2.setSize(500,150);
setVisible(true);
}
public void initnet(){
try{
server = new ServerSocket(6666);
ss = server.accept();
System.out.print("success");
ps = new PrintWriter(ss.getOutputStream());
bf = new BufferedReader(new InputStreamReader(ss.getInputStream()));
s2=bf.readLine();
System.out.print(s2);
tx1.setText("client:"+s2+"\n");
}
catch(IOException e){}
}
class ActionChat implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("cancel")){
try{
ss.close();
ps.close();
bf.close();
}
catch(IOException e2){}
System.exit(-1);
}
else{
//
s1 = tx2.getText();
ps.println(s1);
ps.flush();
tx1.append("Server:"+s1+"\n");
tx2.setText("");
tx2.setEditable(false);
// }
// catch(IOException e1){}
}
}
}
}
//这是服务器端 先是客户端说 然后服务器读 再是服务器说 客户端读
但是出问题了 就是只能实现一次读呀 帮改改 再给说下怎么能实现可以随便一个人可以读写 不用的待对方 (是现在这个给加分)
服务器BufferedReader br 为什么要是成员变量呢 ,不是服务器端的每个socket对象都有一个流么?这样不是所有的socket对象用的是一个流呀(对于3楼的程序) 展开
展开全部
客户端:
package TryService;
import java.io.*;
import java.net.*;
public class SocketClient
{
Socket Client;
BufferedReader getMsg;
BufferedReader br;
PrintStream OutputS;
String str,data,name;
// String ServerIp="192.168.0.111"; //服务器地址
public SocketClient() {
try
{
Client = new Socket("localhost",45537);
System.out.println("^-^连接已经成功!^-^");
}
catch(IOException e)
{
System.out.println("不能连接到服务器:"+e);
return;
}
try{
new Accept(); //收消息
new Send(); //发送消息
}
catch(Exception e){ //捕获异常
System.out.println("消息无法接收或传递!");
}
}
public class Accept extends Thread{ //收消息的线程
public Accept(){
this.start();
}
public void run(){
try
{
getMsg = new BufferedReader(new InputStreamReader(Client.getInputStream()));
while (true)
{
data = getMsg.readLine();
System.out.println(data);
if(data.equals("")) break; //判断是否为空
else
{
continue;
}
}
}
catch(IOException e) //捕获异常
{
System.out.println("接收失败!");
}
}
}
public class Send extends Thread{ //发送消息的线程
public Send(){
this.start();
}
public void run(){
try
{
while (true)
{
OutputS = new PrintStream(Client.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));//从键盘接收数据
str = br.readLine();
if(str.equals("")||(str==null)) //判断是否为空
{
continue;
}
else if(str.equals("quit")){ //退出对话
OutputS.println(str);
break;
}
else {
OutputS.println(str);
}
}
}
catch(IOException e) //捕获异常
{
System.out.println("传送失败!");
}
try
{
System.out.println("连接结束!");
Client.close();
}
catch(IOException e) //捕获异常
{
System.out.println("系统故障!");
}
}
}
public static void main(String args[])
{
new SocketClient(); //测试
}
}
服务器端:
package TryService;
import java.io.*;
import java.net.*;
import java.util.*;
class SocketServer extends Thread {
int count=0;
ServerSocket server;
String str = null;
String data = null;
String showMsg = null;
BufferedReader br,getMsg;
List<Accept> socketList;
public static void main(String args[]) {
new SocketServer();
}
public SocketServer() {
try {
server = new ServerSocket(45537); // 建立一个服务器,用指定的端口port来创建一个侦听Socket
} catch (IOException e) {
System.out.println("服务器未能启动:" + e); // 捕获异常
}
System.out.println("服务器已启动!");
this.start();
}
public void run() {
socketList = new LinkedList<Accept>();
try {
while (true) {
Socket client = server.accept(); // Socket 连接到一个服务器端
count++; //在线用户数统计
Accept s = new Accept(client); // 创建一个线程,自行定义的,具体内容在后面
socketList.add(s);
}
} catch (IOException e) {
System.out.println("服务器故障 !"); // 捕获异常
return;
}
}
void sendAll(String Message) { // 群发的方法体
synchronized (socketList) {
for (Accept a : socketList) {
a.send(Message);
}
}
}
public class Accept extends Thread {
PrintStream ps;
String name = null;
Socket s ;
public Accept(Socket ClientSocket) {
s = ClientSocket;
try { //提醒用户输入名字
ps = new PrintStream(s.getOutputStream());
ps.println("请输入你的名字:(输入的名字不能为空)");
getMsg = new BufferedReader(new InputStreamReader(s.getInputStream()));
name = getMsg.readLine();
} catch (IOException e) { //捕获异常
System.out.println("输入故障!");
}
ps.println("输入已成功!");
this.start();
}
public void send(String Message) {
ps.println(Message);
}
public void run() {
try {
while (true) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
data = br.readLine();
if (data.equals("")||data==null) {
ps.println("发送消息不能为空!");
continue;
}
else if(data.equals("quit")) { //判断用户是否退出了对话
showMsg = "用户" + name + "退出了此次对话!";
count--;
}
else{
showMsg = "用户" + name + "发出的消息:" + data;
}
sendAll(showMsg+" 目前在线用户人数:"+count); // 群发
showMsg = null;
}
} catch (Exception e) {
}
}
}
}
package TryService;
import java.io.*;
import java.net.*;
public class SocketClient
{
Socket Client;
BufferedReader getMsg;
BufferedReader br;
PrintStream OutputS;
String str,data,name;
// String ServerIp="192.168.0.111"; //服务器地址
public SocketClient() {
try
{
Client = new Socket("localhost",45537);
System.out.println("^-^连接已经成功!^-^");
}
catch(IOException e)
{
System.out.println("不能连接到服务器:"+e);
return;
}
try{
new Accept(); //收消息
new Send(); //发送消息
}
catch(Exception e){ //捕获异常
System.out.println("消息无法接收或传递!");
}
}
public class Accept extends Thread{ //收消息的线程
public Accept(){
this.start();
}
public void run(){
try
{
getMsg = new BufferedReader(new InputStreamReader(Client.getInputStream()));
while (true)
{
data = getMsg.readLine();
System.out.println(data);
if(data.equals("")) break; //判断是否为空
else
{
continue;
}
}
}
catch(IOException e) //捕获异常
{
System.out.println("接收失败!");
}
}
}
public class Send extends Thread{ //发送消息的线程
public Send(){
this.start();
}
public void run(){
try
{
while (true)
{
OutputS = new PrintStream(Client.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));//从键盘接收数据
str = br.readLine();
if(str.equals("")||(str==null)) //判断是否为空
{
continue;
}
else if(str.equals("quit")){ //退出对话
OutputS.println(str);
break;
}
else {
OutputS.println(str);
}
}
}
catch(IOException e) //捕获异常
{
System.out.println("传送失败!");
}
try
{
System.out.println("连接结束!");
Client.close();
}
catch(IOException e) //捕获异常
{
System.out.println("系统故障!");
}
}
}
public static void main(String args[])
{
new SocketClient(); //测试
}
}
服务器端:
package TryService;
import java.io.*;
import java.net.*;
import java.util.*;
class SocketServer extends Thread {
int count=0;
ServerSocket server;
String str = null;
String data = null;
String showMsg = null;
BufferedReader br,getMsg;
List<Accept> socketList;
public static void main(String args[]) {
new SocketServer();
}
public SocketServer() {
try {
server = new ServerSocket(45537); // 建立一个服务器,用指定的端口port来创建一个侦听Socket
} catch (IOException e) {
System.out.println("服务器未能启动:" + e); // 捕获异常
}
System.out.println("服务器已启动!");
this.start();
}
public void run() {
socketList = new LinkedList<Accept>();
try {
while (true) {
Socket client = server.accept(); // Socket 连接到一个服务器端
count++; //在线用户数统计
Accept s = new Accept(client); // 创建一个线程,自行定义的,具体内容在后面
socketList.add(s);
}
} catch (IOException e) {
System.out.println("服务器故障 !"); // 捕获异常
return;
}
}
void sendAll(String Message) { // 群发的方法体
synchronized (socketList) {
for (Accept a : socketList) {
a.send(Message);
}
}
}
public class Accept extends Thread {
PrintStream ps;
String name = null;
Socket s ;
public Accept(Socket ClientSocket) {
s = ClientSocket;
try { //提醒用户输入名字
ps = new PrintStream(s.getOutputStream());
ps.println("请输入你的名字:(输入的名字不能为空)");
getMsg = new BufferedReader(new InputStreamReader(s.getInputStream()));
name = getMsg.readLine();
} catch (IOException e) { //捕获异常
System.out.println("输入故障!");
}
ps.println("输入已成功!");
this.start();
}
public void send(String Message) {
ps.println(Message);
}
public void run() {
try {
while (true) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
data = br.readLine();
if (data.equals("")||data==null) {
ps.println("发送消息不能为空!");
continue;
}
else if(data.equals("quit")) { //判断用户是否退出了对话
showMsg = "用户" + name + "退出了此次对话!";
count--;
}
else{
showMsg = "用户" + name + "发出的消息:" + data;
}
sendAll(showMsg+" 目前在线用户人数:"+count); // 群发
showMsg = null;
}
} catch (Exception e) {
}
}
}
}
展开全部
使用多线程了,读写这两个操作变成两个线程,这样就不会阻塞了。思路告诉你,但不好意思,实现我是不能帮你的,自己多查资料。具体实现还是要靠你,对你也是一种锻炼。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要使用线程不断的监听才能在有信息的时候收到。不然的话他当然只执行一次了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询