@Uzb_Dasturchilar Production
By Best Programmer
JAVA: Tarmoq (Network)
Ma’ruza 11
JAVA: SWING
Odatda server dasturlar bironta kompyuterda ishga tushiriladi. Socket kompyuterning
biron bir portiga bog’lanadi. Socket portga bog’langandan keyin kutishga o’tadi, ya’ni
clientlarni ulanishini kutadi.
Client tomonida: Client kompyuter nomini yoki IP adresini va serverda ishlab turgan
dasturning qaysi portni eshitib turganini bilishi kerak. Server bilan muloqatga kirishish
uchun client serverga IP adres va port orqali ulanadi. Client o’z navbatida serverga
kimligini aytib o’tishi lozim. Agar muloqat muvaffaqiyatli ulansa client kompyuterida local
port ochiladi server bilan muloqat qilishi uchun. Local port odatda systema tomonidan
amalga oshiriladi
JAVA: SWING
Client tomonida, agar ulanish qabul qilinsa socket yaratiladi va shu socket orqali server
bilan muloqat qilinadi.
Client va Server bir birinins socketlariga ma’lumot yozib bir biri bilan muloqat qilishi
mumkin.
JAVA: SWING
JAVA: SWING
java.net
java.net.Socket
klass yuqori darajali bo’lib foydalanuvchidan quyi darajali
ma’lumotlarni yashiradi. Platforma darajasida tarmoqlar aro muloq qilish mumkin klass
orqali.
Qo’shimcha sifatida, java.net da
ServerSocket
klasi ham mavjud, bu klass server uchun
bo’lib clientlarni eshitishi(listen) va qabul (accept) qilishi mumkin.
JAVA: SWING
Client tomonidan qaralganda socket quyidagi ko’rinishda ishlaydi. Client serverga ulanadi,
ma’lumot jo’natadi va oladi (agar server jo’natsa).
Server tomondan qaralganda, server socketni 3128 portga ulaydi(bind) va eshitish (listen)
holatga tushadi, yani clientlarni ulanishini kutadi. Yangi ulanishni qabul qilgan server
socketni oqimga yuboradi va o’sha oqim bo’yicha client bilan muloqat qiladi.
JAVA: SWING
// timeout siz socket yaratish
try {
InetAddress addr = InetAddress.getByName("java.sun.com");
int port = 80;
Socket socket = new Socket(addr, port);
} catch (UnknownHostException e) {
} catch (IOException e) {
}
JAVA: SWING
// timeout li socket yaratish
try {
InetAddress addr = InetAddress.getByName("java.sun.com");
int port = 80;
SocketAddress sockaddr = new InetSocketAddress(addr, port);
Socket sock = new Socket();
int timeoutMs = 2000; // 2 seconds
sock.connect(sockaddr, timeoutMs);
} catch (UnknownHostException e) {
} catch (SocketTimeoutException e) {
} catch (IOException e) {
}
JAVA: SWING
try {
int port = 2000;
ServerSocket srv = new ServerSocket(port);
// Clientning ulanishini kutadi.
Socket socket = srv.accept();
} catch (IOException e) {
}
JAVA: SWING
try {
BufferedReader rd = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String str;
while ((str = rd.readLine()) != null) {
process(str);
}
rd.close();
} catch (IOException e) {
}
JAVA: SWING
try {
BufferedWriter wr = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
wr.write("aString");
wr.flush();
} catch (IOException e) {
}
JAVA: SWING
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws Exception {
String host = "host";
int port = 25;
String from = "from@from.net";
String toAddr = "to@to.net";
Socket servSocket = new Socket(host, port);
DataOutputStream os = new
DataOutputStream(servSocket.getOutputStream());
DataInputStream is = new DataInputStream(servSocket.getInputStream());
JAVA: SWING
if (servSocket != null && os != null && is != null) {
os.writeBytes("HELO\r\n");
os.writeBytes("MAIL From:" + from + " \r\n");
os.writeBytes("RCPT To:" + toAddr + "\r\n");
os.writeBytes("DATA\r\n");
os.writeBytes("X-Mailer: Java\r\n");
os.writeBytes("DATE: " +
DateFormat.getDateInstance(DateFormat.FULL,
Locale.US).format(new Date()) + "\r\n");
os.writeBytes("From:" + from + "\r\n");
os.writeBytes("To:" + toAddr + "\r\n");
}
JAVA: SWING
os.writeBytes("Subject:\r\n");
os.writeBytes("body\r\n");
os.writeBytes("\r\n.\r\n");
os.writeBytes("QUIT\r\n");
String responseline;
while ((responseline = is.readUTF()) != null) {
if (responseline.indexOf("Ok") != -1)
break;
}
}
}
Do'stlaringiz bilan baham: |