k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

[Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

2 posters

Go down

Cool [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

Bài gửi by shippou777 Sun Nov 04, 2012 3:47 pm

+ Server xữ lý
Code:

package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class server_lamtoan_udp extends Thread {
   DatagramSocket ds;
   DatagramPacket in,out;
   byte[] nhan = new byte[1000];
   String yeucau,ketqua;
   
   public server_lamtoan_udp(){
      
   }
   
   public server_lamtoan_udp(DatagramSocket d){
      this.ds = d;      
   }
   
   public void receive(){// nhan
      this.in = new DatagramPacket(this.nhan,this.nhan.length);
      try {
         this.ds.receive(this.in);
      } catch (IOException e) {
         e.printStackTrace();
      }
      System.out.println("Da nhan duoc data tu dia chi: "+this.in.getSocketAddress()+", chieu dai chuoi: "+
                     this.in.getLength());
   }
   
   public void send(){// goi
      this.out = new DatagramPacket(this.ketqua.getBytes(),this.ketqua.length(),
            this.in.getAddress(),this.in.getPort());
      try {
         this.ds.send(this.out);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   public void processing(){// xu ly yeu cau de bai
      this.yeucau = new String(this.in.getData(),0,this.in.getLength());
      this.yeucau = this.yeucau.toUpperCase();// doi thanh chu hoa
      this.ketqua = this.yeucau;
   }
   
   public void run(){ // thuc thi
      while(true){
         this.receive();
         this.processing();
         this.send();
      }
   }
   
}

+ Hàm main server
Code:

package program;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import server.server_lamtoan_udp;

public class lamtoan_server_udp_program {

   public static void main(String[] args) {
      int port = 7;
      try {
         DatagramSocket ds = new DatagramSocket(port);
         System.out.println("Da khoi dong server UDP Socket (voi port mac dinh 7)...");
         server_lamtoan_udp sr = new server_lamtoan_udp(ds);
         sr.start();
         
      } catch (SocketException e) {
         System.out.print("Khong tao duoc UDP Socket...");
      }
   }

}

+ Client
Code:

package client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class lamtoan_client_udp {
   int port;
   DatagramSocket ds;
   DatagramPacket in,out;
   InetAddress address;
   byte[] nhan = new byte[1000];
   String yeucau,ketqua;
   
   public lamtoan_client_udp(){
      
   }
   
   public lamtoan_client_udp(DatagramSocket d,InetAddress i,int p){
      this.ds = d;
      this.address = i;
      this.port = p;
   }
   
   public void receive(){// nhan
      this.in = new DatagramPacket(this.nhan,this.nhan.length);
      try {
         this.ds.receive(this.in);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   public void send(){//gui
      Scanner input = new Scanner(System.in);
      System.out.print("Nhap chuoi: ");
      yeucau = input.nextLine();
      this.out = new DatagramPacket(this.yeucau.getBytes(),this.yeucau.length(),
            this.address,this.port);
      try {
         this.ds.send(this.out);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   
   public void processing(){// doc ket qua roi hien ra mam hinh
      this.ketqua = new String(this.in.getData(),0,this.in.getLength());
      System.out.println("Ket qua: "+this.ketqua);
   }
   
   public void run(){// thuc thi
      while(true){
         this.send();
         if(this.yeucau.compareTo("@")==0) break;
         this.receive();
         this.processing();
      }
      System.out.println("Bye bye baby...");
      this.ds.close();
   }
}

+ Hàm main client
Code:

package program;

import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import client.lamtoan_client_udp;

public class lamtoan_client_udp_program {
   
   public static void main(String[] args) {
      int port = 7;//port lang nge cua server
      String ip_server = "192.168.1.107";// dia chi server
      try {
         DatagramSocket ds = new DatagramSocket();
         try {
            InetAddress address = InetAddress.getByName(ip_server);
            System.out.println("Client da ket noi duoc server co ip: "+address);
            lamtoan_client_udp cl = new lamtoan_client_udp(ds,address,port);
            cl.run();
            
         } catch (UnknownHostException e) {
            
         }
      } catch (SocketException e) {
         
      }
   }

}


Code chạy được với nhiều client cùng lúc. Very Happy

Không biết làm sao 1 client cùng lúc gỡi qua server nhiều yêu cầu. Ai biết chỉ với... Sad

[img][You must be registered and logged in to see this link.][/img][img][You must be registered and logged in to see this link.][/img]
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

Bài gửi by Admin Mon Nov 05, 2012 7:27 am

Thiếm làm Server dạng luồn cho chạy không ngừng và chú trọng ngay chỗ này
Code:

while(true)
{
s=ss.accept();// Khi Client được Server chấp nhận
....
....
....
}

Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

Bài gửi by shippou777 Tue Nov 06, 2012 9:00 am

Udp mà bác ơi.. làm gì có s=ss.accept(); [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song. 3884376900
avatar
shippou777

Posts : 460
Thanked : 8
Gia Nhập 11/10/2011

Tài Sản
Thú nuôi:

Về Đầu Trang Go down

Cool Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

Bài gửi by Admin Tue Nov 06, 2012 10:11 am

shippou777 đã viết:Udp mà bác ơi.. làm gì có s=ss.accept(); [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song. 3884376900

Sorry thiếm em nhầm Idea vậy thì dòng lập while(true) là đã đáp ứng yêu cầu rồi.
Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Cool Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.

Bài gửi by Sponsored content


Sponsored content


Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết