ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() +'\n';
outToClient.writeBytes(capitalizedSentence); } } }
3-mavzu
java.net paketining UDP bilan ishlovchi
klasslari
InetAddress
DatagramSocket
DatagramPacket
UDP asosida soketni dasturlash
UDP
oldindan aloqa o’rnatmasdan va ishonchsiz xizmat
Oldindan kelishuv fazasi mavjud emas
Jo’natilgan ma’lumotlar tartibsiz kelishi yoki yo’qolishi mumkin
welcoming socketga ehtiyoj yo’q
Soketlarga oqimlar birlashtirilmaydi
har bir paketga port raqami va qabul qiluvchining IP manzili birlashtirilib paketlar yaratiladi
UDP Sockets
DatagramSocket klassi
DatagramPacket klassi Qabul qilish yoki jo’natish uchun
DatagramSocket metod va
konstruktorlari
Konstruktorlar:
DatagramSocket()
DatagramSocket(int port)
Metodlar:
void receive(DatagramPacket p)
void send(DatagramPacket p)
void close()
DatagramPacket konstruktorlar
Qabul qilish uchun:
DatagramPacket(byte[] buf, int len);
Jo’natish uchun:
DatagramPacket(byte[] buf, int len
InetAddress a, int port);
DatagramPacket metodlari
Byte (bayt tipdagi bir o’lchamli massiv)[] getData();
void setData(byte[] buf);
void setAddress(InetAddress a);
void setPort(int port);
InetAddress getAddress();
int getPort();
Do'stlaringiz bilan baham: |