本人还是新手,希望回答能尽量通俗一些~谢谢各位高人……这是一个服务器程序,主要是产生6个1~10的随机数,发送给客户端。但是通不过……public class heart {
public static void main(String[] args) throws InterruptedException{
DataOutputStream out=null;
ServerSocket numsocket=null;
Socket you=null;
try{
numsocket=new ServerSocket(3288);
}catch(IOException e1){System.out.println("ERRO");}
try{
you=numsocket.accept();
out=new DataOutputStream(you.getOutputStream());
int i;
int number;
for(i=0;i<6;i++){
Random random=new Random();
number=Math.abs(random.nextInt())%10+1;
out.writeUTF(String.valueOf(number));
}
}catch(IOException e){System.out.println(""+e);}
}
}
public static void main(String[] args) throws InterruptedException{
DataOutputStream out=null;
ServerSocket numsocket=null;
Socket you=null;
try{
numsocket=new ServerSocket(3288);
}catch(IOException e1){System.out.println("ERRO");}
try{
you=numsocket.accept();
out=new DataOutputStream(you.getOutputStream());
int i;
int number;
for(i=0;i<6;i++){
Random random=new Random();
number=Math.abs(random.nextInt())%10+1;
out.writeUTF(String.valueOf(number));
}
}catch(IOException e){System.out.println(""+e);}
}
}
int i;
public static void main(String[] args) {
int num[] = null;
String s=null;
Socket numsocket;
DataInputStream in=null;
int i=1;
try{numsocket=new Socket("127.0.0.1",3288);
in =new DataInputStream(numsocket.getInputStream());
for (i=0;i<6;i++){
s=in.readUTF();
num[i]=Integer.parseInt(s);
icon[i]=new ImageIcon(cardroom[num[i]]);
}}
catch(IOException e){}
client cc=new client("客户端");
}
Panel panel1=new Panel();
Panel panel2=new Panel();
Panel panel3=new Panel();
Panel panel4=new Panel();
JLabel lb1=new JLabel();
JLabel lb2=new JLabel();
static Icon icon[];
static String cardroom[]={"G:\\pic\\1.jpg","G:\\pic\\2.jpg","G:\\pic\\3.jpg","G:\\pic\\4.jpg","G:\\pic\\5.jpg","G:\\pic\\6.jpg","G:\\pic\\7.jpg","G:\\pic\\8.jpg","G:\\pic\\9.jpg","G:\\pic\\10.jpg"};
JButton btn1=new JButton(icon[0]);
JButton btn2=new JButton(icon[1]);
JButton btn3=new JButton(icon[2]);
JButton btn4=new JButton(icon[3]);
JButton btn5=new JButton(icon[4]);
JButton btn6=new JButton(icon[5]);
JTextField txt1=new JTextField(40);
JButton btnas=new JButton("出牌");
JButton btntk=new JButton("发送");
TextArea textArea=new TextArea(6,62);
Socket socket=null;
PrintStream printStream=null;
public client(String arg0)throws HeadlessException{
super();
init();
try{
socket=new Socket("localhost",3288);
this.setTitle(lb2.getText());
}
catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
String name=lb2.getText();
PrintStream printStream;
try{
printStream=new PrintStream(socket.getOutputStream());
printStream.println(name);
}catch(IOException e){
e.printStackTrace();
}
}
public void init(){
lb1.setText(" 聊天区域 ");
lb2.setText("abc");
lb1.setPreferredSize(new Dimension(200,20));
btn1.setPreferredSize(new Dimension(200,200));
btn2.setPreferredSize(new Dimension(200,200));
btn3.setPreferredSize(new Dimension(200,200));
btn4.setPreferredSize(new Dimension(200,200));
btn5.setPreferredSize(new Dimension(200,200));
btn6.setPreferredSize(new Dimension(200,200));
// btn1.addActionListener(new Cardplay());
//btn2.addActionListener(new Cardplay());
//btn3.addActionListener(new Cardplay());
//btn4.addActionListener(new Cardplay());
//btn5.addActionListener(new Cardplay());
//btn6.addActionListener(new Cardplay());
btnas.setPreferredSize(new Dimension(80,60));
btnas.setEnabled(false);
btntk.setPreferredSize(new Dimension(80,60));
// btntk.addActionListener(new MyListener());
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
panel2.add(btn4);
panel2.add(btn5);
panel2.add(btn6);
panel1.add(lb1);
panel1.add(textArea);
panel3.add(lb2);
panel3.add(btnas);
panel3.add(txt1);
panel3.add(btntk);
this.add(panel2,BorderLayout.CENTER);
this.add(panel1,BorderLayout.NORTH);
this.add(panel3,BorderLayout.SOUTH);
this.pack();
this.setSize(700,700);
this.setVisible(true);
this.setTitle("DIXIT");//界面设计
}
}
[/color]
public void daemon(){
BufferedReader reader = null;
try
{
InputStream is = m_objChannel.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
m_objChannel.getCOOPProxy().process(new LogMessage(is.toString()));
}catch(Exception e)
{
m_objChannel.getCOOPProxy().process(new LogMessage(e.toString(),e));
this.stop();
}
while(RUNNING_STATE){
try{
String newInput = reader.readLine();
m_objChannel.inputMessage(newInput);
}catch(Exception e){
m_objChannel.getCOOPProxy().process(new LogMessage(e.toString(),e));
this.stop();
}
}
}
[code=Java]
public void daemon(){
BufferedReader reader = null;
try
{
InputStream is = m_objChannel.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
m_objChannel.getCOOPProxy().process(new LogMessage(is.toString()));
}catch(Exception e)
{
m_objChannel.getCOOPProxy().process(new LogMessage(e.toString(),e));
this.stop();
}
while(RUNNING_STATE){
try{
String newInput = reader.readLine();
m_objChannel.inputMessage(newInput);
}catch(Exception e){
m_objChannel.getCOOPProxy().process(new LogMessage(e.toString(),e));
this.stop();
}
}
}
1。关闭防火墙
2。运行 -> cmd -> telnet 127.0.0.1 3288
ok
out.flush();
当然,调用close方法,也可以。