使用java socket编写一个会话程序
1.服务端发送以文本格式存放的20个随机命令:上、下、左、右(最后一个命令为end,具体格式可自行定义);2.客户端显示接受的命令,并由用户按ctrl+方向键响应该命令,...
1.服务端发送以文本格式存放的20个随机命令:上、下、
左、右(最后一个命令为end,具体格式可自行定义);
2.客户端显示接受的命令,并由用户按ctrl+方向键响应该
命令,并向服务器端报告响应情况:若所按方向键与命
令一致则报告“正确”,否则报告“错误”,“”,看到
对方发来“end”则退出;
3.自行设计用户界面;
求高手救救急吧,明天就要交作业了,刚学java没几天实在是不会呀! 展开
左、右(最后一个命令为end,具体格式可自行定义);
2.客户端显示接受的命令,并由用户按ctrl+方向键响应该
命令,并向服务器端报告响应情况:若所按方向键与命
令一致则报告“正确”,否则报告“错误”,“”,看到
对方发来“end”则退出;
3.自行设计用户界面;
求高手救救急吧,明天就要交作业了,刚学java没几天实在是不会呀! 展开
1个回答
2009-06-08
展开全部
//先启动SERVER,再启动GUI
//: Server.java ====================================================
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final int PORT = 65432;
public static final String UP="up",DOWN="down",LEFT="left",RIGHT="right",END="end";
private static final String[] dir = {UP,DOWN,LEFT,RIGHT,};
public static void main(String[] args) throws Exception {
//随机方向发生器
int ccount=20;//命令数
String[] coms = new String[ccount+1];//命令数组
for(int i=0; i<ccount; i++){
int rnd = (int)(Math.random()*4);
coms[i]=dir[rnd];
}
coms[ccount]=END;//最后加入结束标示
//服务器对象
ServerSocket svr = new ServerSocket(PORT);
System.out.println("服务器就绪.");
Socket so=svr.accept();
for(int i=0; i<coms.length; i++){
String command = coms[i];
System.out.println("服务器>> 当前命令 :"+command);
//发送命令
so.getOutputStream().write(command.getBytes());
so.getOutputStream().flush();
//接收回复
byte[] buffer = new byte[64];
int read = so.getInputStream().read(buffer);
String rep = new String(buffer,0,read).trim();
System.out.println("服务器>> 回复结果:"+rep);
}
}
}
//: GUI.java =======================================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class GUI extends JFrame implements KeyListener,ActionListener,Runnable{
private JButton up,down,left,right;
private JLabel comm,rep;
private JPanel center;
private PipedOutputStream pos;
private int count;
private String pc;
GUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
up = new JButton("<html><br/>UP<br/><br/>");
down = new JButton("<html><br>DOWN<br/><br/>");
left = new JButton("LEFT");
right = new JButton("RIGHT");
this.getContentPane().setLayout(new BorderLayout());
add(up,"North");
add(down,"South");
add(left,"West");
add(right,"East");
setSize(400,280);
setVisible(true);
center = new JPanel(new GridLayout(2,1));
comm = new JLabel();
rep=new JLabel();
comm.setHorizontalAlignment(JLabel.CENTER);
comm.setFont(comm.getFont().deriveFont(Font.BOLD,comm.getFont().getSize2D()*1.8f));
rep.setHorizontalAlignment(comm.getHorizontalAlignment());
rep.setFont(comm.getFont());
center.add(rep);
center.add(comm);
add(center);
up.addKeyListener(this);
down.addKeyListener(this);
left.addKeyListener(this);
right.addKeyListener(this);
up.addActionListener(this);
down.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this);
new Thread(this).start();
}
public void run(){
try{
Socket so = new Socket("localhost",Server.PORT);
byte[] buffer = new byte[64];
int read;
System.setIn(new PipedInputStream(pos=new PipedOutputStream()));
while(true){
//接收命令
read = so.getInputStream().read(buffer);
String command = new String(buffer,0,read);
System.out.println("客户端>> 接收命令: "+command);
comm.setText("当前命令("+(++count)+"): "+command);
pc=command;
if(command.equals(Server.END)){
so.getOutputStream().write(Server.END.getBytes());
pos.close();
pos=null;
rep.setText("服务器已关闭!");
break;
}
System.out.println("客户端>> 请输入回复:");
//发送回复
read = System.in.read(buffer);
String tmp = new String(buffer,0,read);
if(tmp.trim().equals(command)){
rep.setForeground(Color.blue);
rep.setText("上次结果("+pc+"): 正确");
}
else{
rep.setForeground(Color.red);
rep.setText("上次结果("+pc+"): 错误");
}
so.getOutputStream().write(tmp.getBytes());
so.getOutputStream().flush();
}
}catch(Exception e){}
}
public static void main(String[] args) throws Exception{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GUI();
}
public void keyPressed(KeyEvent e) {
if(e.isControlDown()){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:up.doClick();break;
case KeyEvent.VK_DOWN:down.doClick();break;
case KeyEvent.VK_LEFT:left.doClick();break;
case KeyEvent.VK_RIGHT:right.doClick();break;
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {
Object s = e.getSource();
String ss="";
if(s==up) ss=Server.UP;
else if(s==down) ss=Server.DOWN;
else if(s==left) ss=Server.LEFT;
else if(s==right) ss=Server.RIGHT;
try {
if(pos!=null)
pos.write(ss.getBytes());
} catch (IOException e1) {e1.printStackTrace();}
}
}
//: Server.java ====================================================
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final int PORT = 65432;
public static final String UP="up",DOWN="down",LEFT="left",RIGHT="right",END="end";
private static final String[] dir = {UP,DOWN,LEFT,RIGHT,};
public static void main(String[] args) throws Exception {
//随机方向发生器
int ccount=20;//命令数
String[] coms = new String[ccount+1];//命令数组
for(int i=0; i<ccount; i++){
int rnd = (int)(Math.random()*4);
coms[i]=dir[rnd];
}
coms[ccount]=END;//最后加入结束标示
//服务器对象
ServerSocket svr = new ServerSocket(PORT);
System.out.println("服务器就绪.");
Socket so=svr.accept();
for(int i=0; i<coms.length; i++){
String command = coms[i];
System.out.println("服务器>> 当前命令 :"+command);
//发送命令
so.getOutputStream().write(command.getBytes());
so.getOutputStream().flush();
//接收回复
byte[] buffer = new byte[64];
int read = so.getInputStream().read(buffer);
String rep = new String(buffer,0,read).trim();
System.out.println("服务器>> 回复结果:"+rep);
}
}
}
//: GUI.java =======================================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class GUI extends JFrame implements KeyListener,ActionListener,Runnable{
private JButton up,down,left,right;
private JLabel comm,rep;
private JPanel center;
private PipedOutputStream pos;
private int count;
private String pc;
GUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
up = new JButton("<html><br/>UP<br/><br/>");
down = new JButton("<html><br>DOWN<br/><br/>");
left = new JButton("LEFT");
right = new JButton("RIGHT");
this.getContentPane().setLayout(new BorderLayout());
add(up,"North");
add(down,"South");
add(left,"West");
add(right,"East");
setSize(400,280);
setVisible(true);
center = new JPanel(new GridLayout(2,1));
comm = new JLabel();
rep=new JLabel();
comm.setHorizontalAlignment(JLabel.CENTER);
comm.setFont(comm.getFont().deriveFont(Font.BOLD,comm.getFont().getSize2D()*1.8f));
rep.setHorizontalAlignment(comm.getHorizontalAlignment());
rep.setFont(comm.getFont());
center.add(rep);
center.add(comm);
add(center);
up.addKeyListener(this);
down.addKeyListener(this);
left.addKeyListener(this);
right.addKeyListener(this);
up.addActionListener(this);
down.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this);
new Thread(this).start();
}
public void run(){
try{
Socket so = new Socket("localhost",Server.PORT);
byte[] buffer = new byte[64];
int read;
System.setIn(new PipedInputStream(pos=new PipedOutputStream()));
while(true){
//接收命令
read = so.getInputStream().read(buffer);
String command = new String(buffer,0,read);
System.out.println("客户端>> 接收命令: "+command);
comm.setText("当前命令("+(++count)+"): "+command);
pc=command;
if(command.equals(Server.END)){
so.getOutputStream().write(Server.END.getBytes());
pos.close();
pos=null;
rep.setText("服务器已关闭!");
break;
}
System.out.println("客户端>> 请输入回复:");
//发送回复
read = System.in.read(buffer);
String tmp = new String(buffer,0,read);
if(tmp.trim().equals(command)){
rep.setForeground(Color.blue);
rep.setText("上次结果("+pc+"): 正确");
}
else{
rep.setForeground(Color.red);
rep.setText("上次结果("+pc+"): 错误");
}
so.getOutputStream().write(tmp.getBytes());
so.getOutputStream().flush();
}
}catch(Exception e){}
}
public static void main(String[] args) throws Exception{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GUI();
}
public void keyPressed(KeyEvent e) {
if(e.isControlDown()){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:up.doClick();break;
case KeyEvent.VK_DOWN:down.doClick();break;
case KeyEvent.VK_LEFT:left.doClick();break;
case KeyEvent.VK_RIGHT:right.doClick();break;
}
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {
Object s = e.getSource();
String ss="";
if(s==up) ss=Server.UP;
else if(s==down) ss=Server.DOWN;
else if(s==left) ss=Server.LEFT;
else if(s==right) ss=Server.RIGHT;
try {
if(pos!=null)
pos.write(ss.getBytes());
} catch (IOException e1) {e1.printStackTrace();}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询