要完成一个java服务器,要求一个客户端能通过服务器向别的客户端发送“hello”,也能接收“hello”
服务器的代码在“http://zhidao.baidu.com/question/419305670.html?quesup2&oldq=1#share-panel”这里...
服务器的代码在“http://zhidao.baidu.com/question/419305670.html?quesup2&oldq=1#share-panel”这里得到帮助了,就使用的那个。我原先的两个客户端一个只能发送,一个只能接收,所以来求助高手帮我弄一个客户端代码,既能接收别的客户端的信息,也能向别的客户端发送信息,只需要完成这样的基本的功能即可,不需要像那些聊天客户端那样设计界面什么的。我是新手,求指教~
这是我现在有的两个客户端代码:
发送:
public class Client1 {
Socket socket;
BufferedReader in;
PrintWriter out;
public Client1() {
try {
socket = new Socket("localhost", 3333);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader line = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String tmp = line.readLine();
if ("exit1".equalsIgnoreCase(tmp)) {
break;
}
out.println(tmp);
}
line.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
new Client1();
}
}
接收:
public class Client2 {
Socket socket;
PrintWriter out;
BufferedReader in;
public Client2() {
try {
socket = new Socket("localhost", 3333);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
String abc ;
abc = in.readLine();
System.out.println(abc);
}
}catch (IOException e) {
System.out.println(e.getMessage());}
}
public static void main(String[] args) throws IOException {
new Client();
}
} 展开
这是我现在有的两个客户端代码:
发送:
public class Client1 {
Socket socket;
BufferedReader in;
PrintWriter out;
public Client1() {
try {
socket = new Socket("localhost", 3333);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader line = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String tmp = line.readLine();
if ("exit1".equalsIgnoreCase(tmp)) {
break;
}
out.println(tmp);
}
line.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
new Client1();
}
}
接收:
public class Client2 {
Socket socket;
PrintWriter out;
BufferedReader in;
public Client2() {
try {
socket = new Socket("localhost", 3333);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
String abc ;
abc = in.readLine();
System.out.println(abc);
}
}catch (IOException e) {
System.out.println(e.getMessage());}
}
public static void main(String[] args) throws IOException {
new Client();
}
} 展开
2个回答
展开全部
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Client1 extends Frame implements Runnable{
Socket socket;
BufferedReader in;
PrintWriter out;
private TextField textfield = new TextField();//输入文本框 就是你想从键盘输入的数据先写在这
private TextArea textarea = new TextArea();//对话框 显示客户端之间的聊天信息
public Client1() {
super("聊天窗口");//初始化窗口标题
setSize(300,200);//设置窗口大小
setLayout(new BorderLayout());//布局
add(BorderLayout.SOUTH, textfield);
add(BorderLayout.CENTER, textarea);
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendChat(textfield.getText());//向服务器发送数据
}
});//增加监听器 当你按下回车键时就执行
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
try{
if(in!=null)in.close();
if(out!=null)out.close();
if(socket!=null)socket.close();
}catch(IOException e){
}
System.exit(0);//退出程序
}
});//增加关闭窗口的监听器 当点击窗口的X时关闭窗口
try{
socket = new Socket("localhost", 3333);
out = new PrintWriter(socket.getOutputStream(), true);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(Exception e){
e.printStackTrace();
}
new Thread(this).start();//用一个线程去接收从服务器转发的其他客户端的数据
setVisible(true);
}
//sendChat方法用于向服务器发送数据
public void sendChat(String message) {
out.println(message);
textfield.setText("");//输入框清空
}
public void run(){
String message=null;
try {
while (true) {
message = in.readLine();
textarea.append(message + "\n");//把从服务器转发来的其他客户端信息追加到对话框
}
} catch (IOException e) {System.out.println(e);}
}
public static void main(String[] args) {
new Client1();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Client1 extends Frame implements Runnable{
Socket socket;
BufferedReader in;
PrintWriter out;
private TextField textfield = new TextField();//输入文本框 就是你想从键盘输入的数据先写在这
private TextArea textarea = new TextArea();//对话框 显示客户端之间的聊天信息
public Client1() {
super("聊天窗口");//初始化窗口标题
setSize(300,200);//设置窗口大小
setLayout(new BorderLayout());//布局
add(BorderLayout.SOUTH, textfield);
add(BorderLayout.CENTER, textarea);
textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendChat(textfield.getText());//向服务器发送数据
}
});//增加监听器 当你按下回车键时就执行
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
try{
if(in!=null)in.close();
if(out!=null)out.close();
if(socket!=null)socket.close();
}catch(IOException e){
}
System.exit(0);//退出程序
}
});//增加关闭窗口的监听器 当点击窗口的X时关闭窗口
try{
socket = new Socket("localhost", 3333);
out = new PrintWriter(socket.getOutputStream(), true);
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(Exception e){
e.printStackTrace();
}
new Thread(this).start();//用一个线程去接收从服务器转发的其他客户端的数据
setVisible(true);
}
//sendChat方法用于向服务器发送数据
public void sendChat(String message) {
out.println(message);
textfield.setText("");//输入框清空
}
public void run(){
String message=null;
try {
while (true) {
message = in.readLine();
textarea.append(message + "\n");//把从服务器转发来的其他客户端信息追加到对话框
}
} catch (IOException e) {System.out.println(e);}
}
public static void main(String[] args) {
new Client1();
}
}
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |