Java by API/java.net/Socket

Материал из Java эксперт
Перейти к: навигация, поиск

new Socket(InetAddress address, int port) throws IOException

   <source lang="java">
 

import java.net.InetAddress; import java.net.Socket; public class Main {

 public static void main(String[] argv) throws Exception {
   InetAddress addr = InetAddress.getByName("java.sun.ru");
   int port = 80;
   Socket socket = new Socket(addr, port);
 }

}


 </source>
   
  
 
  



Socket: connect(SocketAddress endpoint, int timeout) throws IOException

   <source lang="java">
 

import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; public class Main {

 public static void main(String[] argv) throws Exception {
   InetAddress addr = InetAddress.getByName("java.sun.ru");
   int port = 80;
   SocketAddress sockaddr = new InetSocketAddress(addr, port);
   Socket sock = new Socket();
   int timeoutMs = 2000; // 2 seconds
   sock.connect(sockaddr, timeoutMs);
 }

}


 </source>
   
  
 
  



Socket: getInputStream() throws IOException

   <source lang="java">
 

import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; class Whois {

 public static void main(String args[]) throws Exception {
   int c;
   Socket s = new Socket("internic.net", 43);
   InputStream in = s.getInputStream();
   OutputStream out = s.getOutputStream();
   String str = "asdfasdfasdf\n";
   byte buf[] = str.getBytes();
   out.write(buf);
   while ((c = in.read()) != -1) {
     System.out.print((char) c);
   }
   s.close();
 }

}


 </source>
   
  
 
  



Socket: getLocalSocketAddress()

   <source lang="java">

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class MainClass {

 public static void main(String[] args) throws Exception {
   String serverName = args[0];
   int port = Integer.parseInt(args[1]);
   try {
     System.out.println("Connecting to " + serverName + " on port " + port);
     Socket client = new Socket(serverName, port);
     System.out.println("Just connected to " + client.getRemoteSocketAddress());
     OutputStream outToServer = client.getOutputStream();
     DataOutputStream out = new DataOutputStream(outToServer);
     out.writeUTF("Hello from " + client.getLocalSocketAddress());
     InputStream inFromServer = client.getInputStream();
     DataInputStream in = new DataInputStream(inFromServer);
     System.out.println("Server says " + in.readUTF());
     client.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

}


 </source>
   
  
 
  



Socket: getOutputStream() throws IOException

   <source lang="java">
 

import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; class Whois {

 public static void main(String args[]) throws Exception {
   int c;
   Socket s = new Socket("internic.net", 43);
   InputStream in = s.getInputStream();
   OutputStream out = s.getOutputStream();
   String str = "asdfasdfasdf\n";
   byte buf[] = str.getBytes();
   out.write(buf);
   while ((c = in.read()) != -1) {
     System.out.print((char) c);
   }
   s.close();
 }

}


 </source>
   
  
 
  



Socket: getRemoteSocketAddress()

   <source lang="java">

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class MainClass {

 public static void main(String[] args) throws Exception {
   String serverName = args[0];
   int port = Integer.parseInt(args[1]);
   try {
     System.out.println("Connecting to " + serverName + " on port " + port);
     Socket client = new Socket(serverName, port);
     System.out.println("Just connected to " + client.getRemoteSocketAddress());
     OutputStream outToServer = client.getOutputStream();
     DataOutputStream out = new DataOutputStream(outToServer);
     out.writeUTF("Hello from " + client.getLocalSocketAddress());
     InputStream inFromServer = client.getInputStream();
     DataInputStream in = new DataInputStream(inFromServer);
     System.out.println("Server says " + in.readUTF());
     client.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

}


 </source>
   
  
 
  



Socket: readUTF() and writeUTF(String str)

   <source lang="java">

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class MainClass extends Thread {

 public MainClass() throws IOException {
 }
 public void run() {
   try {
     Socket socket = new Socket("127.0.0.1", 2000);
     DataInputStream in = new DataInputStream(socket.getInputStream());
     BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
     DataOutputStream out = new DataOutputStream(socket.getOutputStream());
     while (true) {
       System.out.print("Enter response: ");
       String response = console.readLine();
       out.writeUTF(response);
       String message = in.readUTF();
       System.out.println(message);
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 public static void main(String[] args) {
   try {
     Thread t = new MainClass();
     t.start();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }

}


 </source>