Java net paketi tcp va udp


Download 353.64 Kb.
bet2/12
Sana14.01.2023
Hajmi353.64 Kb.
#1092465
1   2   3   4   5   6   7   8   9   ...   12
Bog'liq
Tarmoq dasturlash

Soket metodlari
socket (): soketni yaratish
bind(): port# va IP adres soketi bilan birlashtirish
listen(): aloqa o’rnatilishini passiv holatda eshitish
connect(): soket bilan bog’lash
accept(): yangi aloqani tasdiqlash
write(): soketga ma’lumot yozish
read(): soketdan ma’lumotni o’qib olish
sendto(): UDP soketga datagram jo’natish
recvfrom(): UDP soketdan datagramni o’qish
close(): soketni yopish
Soket klassi konstruktorlari : Socket(InetAddress server, int port);
Socket(String hostname, int port);


ServerSocket klassi konstruktorlari
ServerSocket(int port);
ServerSocket(int port, int backlog);
Socket I/O
java.io paketi qo’llab-quvvatlaydi
InputStream va OutputStream klasslari
InputStream asoslari
bir baytlarni o’qib b massiviga joylashtiradi
int read(byte[] b);
return –1 on EOF.
OutputStream Basics
b.length baytlarni yozadi
void write(byte[] b);
TCPClient.java
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“localhost", 6789); DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream())
BufferedReader inFromServer = newBufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close(); } }
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;

Download 353.64 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling