.


:




:

































 

 

 

 


Java

 

, , Java. , , .

?

(instant messanger) . (MSN, ICQ, Skype . .), . , . , . , . ""?

IP-. . IP-, , IP- (, 64.104.137.158). , , (, www.quizful.net). , IP- .

. , 10 , , . : 10 1 . ? , , , . , . , , , , IP-, . IP- , ? ! . , ? ! " " . , , , . ( ). . : IP-, , -, , . , ( , ?). , , , :

IP- = .
- = , .

, :

IP- + - = _____

:

_____ = .

. , Google .

_____ !

, IP- . , . 64.104.137.58:80, 64.104.137.58 IP- 80 .

?

, . Java, . :

1) Java ( -, ). , .

2) , .

3) ;t ( , ).

4) , , . ? . , Java , , . 2 Server.java Client.java. , , .

Server.java


import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] ar) {
int port = 6666; // ( 1025 65535)
try {
ServerSocket ss = new ServerSocket(port); //
System.out.println("Waiting for a client...");

Socket socket = ss.accept(); // -
System.out.println("Got a client:)... Finally, someone saw me through all the cover!");
System.out.println();

// , .
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();

// , .
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);

String line = null;
while(true) {
line = in.readUTF(); // .
System.out.println("The dumb client just sent me this line: " + line);
System.out.println("I'm sending it back...");
out.writeUTF(line); // .
out.flush(); // .
System.out.println("Waiting for the next line...");
System.out.println();
}
} catch(Exception x) { x.printStackTrace(); }
}
}

Client.java

import java.net.*;
import java.io.*;

public class Client {
public static void main(String[] ar) {
int serverPort = 6666; // .
String address = "127.0.0.1"; // IP- , .
// .

try {
InetAddress ipAddress = InetAddress.getByName(address); // IP-.
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort); // IP- .
System.out.println("Yes! I just got hold of the program.");

// , .
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();

// , .
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);

// .
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();

while (true) {
line = keyboard.readLine(); // - Enter.
System.out.println("Sending this line to the server...");
out.writeUTF(line); // .
out.flush(); // .
line = in.readUTF(); // .
System.out.println("The server was very polite. It sent me this: " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
} catch (Exception x) {
x.printStackTrace();
}
}
}

:

javac Server.java Client.java

(DOS). :

java Server

:

java Client

.

, , Enter. . , Ctrl-C .

. , , .

:

ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();

ServerSocket Socket. Socket . ServerSocket , . , , , accept(). . , . , Socket , . , Socket . , .

Socket . Socket , IP- . InetAddress IP- ( ). InetAddress :

InetAddress ipAddress = InetAddress.getByName(address);

, 127.0.0.1. . , IP- .

InetAddress, Socket:

Socket socket = new Socket(ipAddress, serverPort);

Socket , . , .

InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();

. String . .

DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);

( ). , , . , .

 

http://www.quizful.net/post/java-socket-programming



<== | ==>
IV . | , .
:


: 2016-11-22; !; : 742 |


:

:

, .
==> ...

1497 - | 1404 -


© 2015-2024 lektsii.org - -

: 0.026 .