⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firstsocket.java

📁 exemple for using java chat client + server
💻 JAVA
字号:
/*
 * firstsocket.java
 *
 * Created on 9 octobre 2008, 16:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package reseaux;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Enumeration;

/**
 *
 * @author Administrateur
 */
public class firstsocket {
    
    /** Creates a new instance of firstsocket */
    public firstsocket() {
    }
//   public static void main(String[] args) throws UnknownHostException, IOException {
//    InetAddress adress = InetAddress.getByName("www.google.com");
//    Socket s =new Socket(adress,80);
//    String g = "GET / HTTP/1.1\n" +
//     "Host: www.google.com\n\n";
//    OutputStream ostream = s.getOutputStream();
//    ostream.write(g.getBytes());
//    
//    InputStream istream = s.getInputStream();
//    byte [] b = new byte [1000];
//    int bitrecu = istream.read(b);
//     if(bitrecu>0) {
//   System.out.println("On a recu : " + bitrecu + " bits");
//   System.out.println("Recu : " + new String(b,0, bitrecu));
//    
//}
//}
     public static void main(String[] args) throws IOException {
  Socket s = new Socket("www.developpez.com", 80);
  //recup閞ation des flux
  OutputStream oStream = s.getOutputStream();;
  InputStream iStream = s.getInputStream();;
  byte[] b = new byte[1000];
  String g = "GET / HTTP/1.1\n" + "Host: www.developpez.com\n\n";
   
  try { 
   
   oStream.write(g.getBytes());
   int bitsRecus = 0;
   while((bitsRecus = iStream.read(b)) >= 0) {
   
    System.out.println("On a recu : " + bitsRecus + " bits");
    System.out.println("Recu : " + new String(b, 0, bitsRecus));
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   
   //fermeture des flux et des sockets
   oStream.close();
   iStream.close();
   s.close();
  }
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -