소켓 네트워크 인 터 랙 션 간단 한 예제

자바 의 네트워크 정보 전송 방식 은 TCP 프로 토 콜 이나 UD 프로 토 콜 P 를 기반 으로 하고 socket 은 TCP 프로 토 콜 을 기반 으로 합 니 다.



  1
(1)     :
import java.io.*;
import java.net.*;
public class Client
{  public static void main(String args[])
   {  String s=null;
      Socket mysocket;
      DataInputStream in=null;
      DataOutputStream out=null;
      try{
          mysocket=new Socket("localhost",4331);
          in=new DataInputStream(mysocket.getInputStream());
          out=new DataOutputStream(mysocket.getOutputStream()); 
          out.writeUTF("  !");//   out "  "    。
          while(true)
            { 
               s=in.readUTF();//    in       "  "    。    ,
                             //       。
               out.writeUTF(":"+Math.random());
               System.out.println("    :"+s);
               Thread.sleep(500);
            } 
         }
       catch(IOException e)
         {  System.out.println("    ");
         }
       catch(InterruptedException e){}
   } 
}
(2)      :
import java.io.*;import java.net.*;
public class Server
{ public static void main(String args[])
  {  ServerSocket server=null;
     Socket you=null;String s=null;
     DataOutputStream out=null;DataInputStream  in=null;
     try{ server=new ServerSocket(4331);}
      catch(IOException e1){System.out.println("ERRO:"+e1);} 
     try{  you=server.accept();
           in=new DataInputStream(you.getInputStream());
           out=new DataOutputStream(you.getOutputStream());
           while(true)
           {
             s=in.readUTF();//     in      "  "    。    ,
                             //       。

             out.writeUTF("  :     ");//   out "  "    .
             out.writeUTF("     :"+s);
             System.out.println("     :"+s);
             Thread.sleep(500);
           }
        }
     catch(IOException e)
         {  System.out.println(""+e);
         }
       catch(InterruptedException e){}
   }
}

  (2)
(1)    
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Computer_client extends Applet implements Runnable,ActionListener
{  Button   ;TextField          ,       ;
   Socket socket=null;
   DataInputStream in=null; DataOutputStream out=null;
   Thread thread; 
   public void init()
   {  setLayout(new GridLayout(2,2));
      Panel p1=new Panel(),p2=new Panel(); 
        =new Button("   ");
               =new TextField(12);       =new TextField(12);
      p1.add(new Label("          ,        :"));
    p1.add(          );
      p2.add(new Label("    :"));p2.add(       );p2.add(  );
        .addActionListener(this);
      add(p1);add(p2);
   }
   public void start()
   {  try
         {  //                  :
            socket = new Socket(this.getCodeBase().getHost(), 4331); 
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
         } 
      catch (IOException e){}
      if(thread == null)
        {  thread = new Thread(this);
           thread.start();
        }
   }
   public void run()
   {  String s=null;
      while(true)
       {    try{  s=in.readUTF();//    ,       。

                         .setText(s);
               }
           catch(IOException e) 
{         .setText("       ");
   break;
}   
       }
  }
  public void actionPerformed(ActionEvent e)
  {  if(e.getSource()==  )
      {  String s=         .getText();
         if(s!=null)
           {  try {  out.writeUTF(s);
                  }
              catch(IOException e1){} 
           }               
     }
  }
}

 (2)     
import java.io.*;import java.net.*;
import java.util.*;import java.sql.*;
public class Computer_server 
{  public static void main(String args[])
   {  ServerSocket server=null;Server_thread thread;
      Socket you=null;
      while(true) 
       {  try{  server=new ServerSocket(4331);
             }
          catch(IOException e1) 
             {  System.out.println("    "); //ServerSocket        。
             } 
          try{  you=server.accept();
                System.out.println("     :"+you.getInetAddress());
             }
         catch (IOException e)
             {  System.out.println("      ");
             }
         if(you!=null) 
             {  new Server_thread(you).start(); //              。  
             }
         else 
             {  continue;
             }
      }
   }
}
class Server_thread extends Thread
{  Socket socket;Connection Con=null;Statement Stmt=null;
   DataOutputStream out=null;DataInputStream  in=null;int n=0;
   String s=null;
   Server_thread(Socket t)
   {  socket=t;
      try {  in=new DataInputStream(socket.getInputStream());
             out=new DataOutputStream(socket.getOutputStream());
          }
      catch (IOException e)
          {}
   }  
   public void run()        
   {  while(true)
      {  double a[]=new double[3] ;int i=0;
         try{  s=in.readUTF();    ,       。

    StringTokenizer fenxi=new StringTokenizer(s," ,");
                 while(fenxi.hasMoreTokens())
                   {  String temp=fenxi.nextToken();
                      try{  a[i]=Double.valueOf(temp).doubleValue();i++;
                         }
                      catch(NumberFormatException e)
  {  out.writeUTF("       ");
                         }
                   }
                double p=(a[0]+a[1]+a[2])/2.0;
                out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
                sleep(2);    
            }
         catch(InterruptedException e){}
         catch (IOException e) 
            {  System.out.println("    ");
  break;
            }
      }
   } 
}


이 예 들 은 모두 자바 2 실 용 튜 토리 얼 에 있 는 것 이다.

좋은 웹페이지 즐겨찾기