Java by API/java.net/Socket
Содержание
- 1 new Socket(InetAddress address, int port) throws IOException
- 2 Socket: connect(SocketAddress endpoint, int timeout) throws IOException
- 3 Socket: getInputStream() throws IOException
- 4 Socket: getLocalSocketAddress()
- 5 Socket: getOutputStream() throws IOException
- 6 Socket: getRemoteSocketAddress()
- 7 Socket: readUTF() and writeUTF(String str)
new Socket(InetAddress address, int port) throws IOException
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);
}
}
Socket: connect(SocketAddress endpoint, int timeout) throws IOException
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);
}
}
Socket: getInputStream() throws IOException
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();
}
}
Socket: getLocalSocketAddress()
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();
}
}
}
Socket: getOutputStream() throws IOException
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();
}
}
Socket: getRemoteSocketAddress()
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();
}
}
}
Socket: readUTF() and writeUTF(String str)
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();
}
}
}