各位大哥好老师让我们写一个server发消息给client的程序我写了一点 不知道怎样 能让server发个消息 比如it is your turn 然后client的屏幕上可以显示这个话 请在我程序上添加 谢谢一共两个类 一个TestClient.java 一个TestServer.java/////////////////////////////////////////////////////////////
//Server class TestServer.java
/////////////////////////////////////////////////////////////
import java.net.*;
import java.io.*;public class TestServer
{
private final int PORT=3000; //Port number
private PrintWriter out=null;
private BufferedReader in=null; public TestServer()
{//Constructor try
{
ServerSocket ss = new ServerSocket(PORT);
for(;;)   // accept multiple clients
{
if (clientNum==3){break;}
Socket clientSoc=ss.accept();
System.out.println("New client connection" );
ThreadHandle m = new ThreadHandle(clientSoc,clientNum);
m.start();
clientNum++; //Now get the 'streams'
out=new PrintWriter(clientSoc.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(clientSoc.getInputStream()));
out.println("you are Client" + clientNum);
} //while(true)
//{//Infinite loop //String s=in.readLine(); //reads line 'from client'
//if(s==null)break;//Abort if null string
//System.out.println(s); //Prints it out
//}
System.out.println("MAX CLIENTS");
Game  g= new Game();
} catch(Exception e)
{
e.printStackTrace(); //Displays Error if things go wrong....
}
} public static void main(String args[])
{
new TestServer(); //Makes object (calls constructor)
} private int clientNum=0;
}
class ThreadHandle extends Thread
{
public ThreadHandle(Socket clientSoc, int num)
{
number=num;
//System.out.println("new client" + number);
}
public void run()
{
System.out.println("new client" + number); }
private int number;
}class Game
{
public Game()
{
System.out.println("Game Is started");
}
}
 /////////////////////////////////////////////////////////////
//Client class TestClient.java
/////////////////////////////////////////////////////////////
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.util.Random;public class TestClient extends Thread
{ private final int PORT=3000; //Port number
private PrintWriter out=null;
private BufferedReader in=null;
private Socket mysocket;
private DiceValue mydice = new DiceValue(); public TestClient()
{//Constructor
JFrame frame = new SandL(mydice);
        frame.setVisible(true); try
{
//Try to connect to server at IP address/port combination
Socket soc=new Socket("127.0.0.1",PORT);
//Now get the 'streams' - you use these to send messages
out=new PrintWriter(soc.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
out.println("Hello!\n");
while(true)
{//Infinite loop String s=in.readLine(); //reads line 'from client'
if(s==null)break;//Abort if null string
System.out.println(s); //Prints it out
mydice.SetVal(4);
}
}
catch(Exception e)
{
e.printStackTrace(); //Displays Error if things go wrong....
}
} public static void main(String args[])
{
   new TestClient(); //Makes object (calls constructor)
}
}class SandL extends JFrame implements ActionListener
{
private DiceValue mydice;    public SandL(DiceValue Diceval)    {
mydice=Diceval;
        setTitle("Snakes and Ladders");
        setSize(675, 670);
        addWindowListener(new WindowAdapter()
        {            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        }); //load image onto frame
        Container contentPane = getContentPane();
        panel = new ImageLoaderPanel(this);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(panel, BorderLayout.CENTER);         dice = new JButton("Roll Dice");
        //ButtonPanel dice = new ButtonPanel(this);
contentPane.add(dice, BorderLayout.PAGE_END);
dice.addActionListener(this); JLabel label2 = new JLabel("hello1" + mydice.ReturnVal());
//label2.setPreferredSize(new Dimension(200, 50)); contentPane.add(label2,BorderLayout.LINE_START); JLabel label3 = new JLabel("hello2");
//label2.setPreferredSize(new Dimension(200, 50)); contentPane.add(label3,BorderLayout.LINE_END);    }    public void actionPerformed(ActionEvent evt)
    {
        Object source = evt.getSource();
        if (source==dice){repaint();System.out.println("helloe");}    }
    private ImageLoaderPanel panel;
    private JButton dice;
    private JMenuItem exitItem;
    private JMenuItem gminput;
    private JMenuItem gmshuffle;
    private JMenuItem gmstart;
    private JMenuItem habout;}class ImageLoaderPanel extends JPanel
{    public ImageLoaderPanel(SandL frame)
    { try
{
    img = ImageIO.read(new File("board.gif"));
}
catch (IOException e)
{
System.out.println("no image");
}
}
public void paintComponent(Graphics g)
    {
super.paintComponent(g);
       if (img != null)
       {
          g.drawImage(img, 0, 0, null);
  }
    }    private BufferedImage img = null;
}class DiceValue extends JPanel
{
public DiceValue()
    {}
public int ReturnVal(){return Diceval;}
public void SetVal(int dice){Diceval=dice;}   private int Diceval=1;
}

解决方案 »

  1.   

    我给你贴一个Sever与Client实现简单通信的代码吧
    import java.io.*;
    import java.net.*;
    import java.applet.Applet;
    public class talkserver
    {
    public static void main(String args[])
    {
    try
    {
    ServerSocket server = null;
    try
    {
    server = new ServerSocket(4700);
    }catch(Exception e)
    {
    System.out.println("can not listen to:" + e);
    }
    Socket socket = null;
    try
    {
    socket = server.accept();
    }catch(Exception e)
    {
    System.out.println("Error:" + e);
    }
    String line;
    BufferedReader is = new BufferedReader(new InputStreamReader(
    socket.getInputStream()));
    PrintWriter os = new PrintWriter(socket.getOutputStream());
    BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Client:" + is.readLine());
    line = sin.readLine();
    while (!line.equals("bye"))
    {
    os.println(line);
    os.flush();
    System.out.println("Server:" + line);
    System.out.println("Client:" + is.readLine());
    line = sin.readLine();
    } is.close();
    os.close();
    socket.close();
    server.close();
    }catch(Exception e)
    {
    System.out.println("Error" + e);
    }
    }
    }
    import java.io.*;
    import java.net.*;
    public class talkclient
    {
    public static void main(String args[])
    {
    try
    {
    Socket socket = new Socket("127.0.0.1",4700);
    BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter os = new PrintWriter(socket.getOutputStream());
    BufferedReader is = new BufferedReader(new InputStreamReader(
    socket.getInputStream()));
    String readline;
    readline = sin.readLine();
    while (!readline.equals("bye"))
    {
    os.println(readline);
    os.flush();
    System.out.println("Client:" + readline);
    System.out.println("Server:" + is.readLine());
    readline = sin.readLine();
    }
    os.close();
    is.close();
    socket.close();
    }catch(Exception e)
    {
    System.out.println("Error" + e);
    }
    }
    }
      

  2.   

    http://topic.csdn.net/u/20090114/14/07d91503-b95a-46ca-bf92-49f34364db5f.html
    一楼有个例子~~
      

  3.   

    ThreadHandle这个类是用于完成服务器端的响应
    在响应结束之前一直要维持客户端的连接信息,所以追加了一个Socket成员变量class ThreadHandle extends Thread
    {
        public ThreadHandle(Socket clientSoc, int num)
        {
            number=num;
            //System.out.println("new client" + number);        this.clientSoc = clientSoc //  保存accept后的Socket
        }
        public void run()
        {
            System.out.println("new client" + number);
            // 打开clientSoc,向Socket中写入消息    }
        private Socket clientSoc; 
        private int number;
    }