[Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.
2 posters
Trang 1 trong tổng số 1 trang
[Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.
+ Server xữ lý
Code chạy được với nhiều client cùng lúc.
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...
[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]
- 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();
}
}
}
- 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...");
}
}
}
- 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();
}
}
- 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.
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...
[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]
shippou777- Posts : 460
Thanked : 8
11/10/2011
Tài Sản
Thú nuôi:
Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.
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
....
....
....
}
Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.
Udp mà bác ơi.. làm gì có s=ss.accept();
shippou777- Posts : 460
Thanked : 8
11/10/2011
Tài Sản
Thú nuôi:
Re: [Java][UDP] Client_Server đổi chử thường thành hoa phục vụ song song.
shippou777 đã viết:Udp mà bác ơi.. làm gì có s=ss.accept();
Sorry thiếm em nhầm vậy thì dòng lập while(true) là đã đáp ứng yêu cầu rồi.
Similar topics
» Tổng Quan Về HĐH Symbian Và Một Số Lỗi Thường Găp.
» 7 bài tập về java Have Full
» 653 Game Java Cho Nokia ne`!
» Update Tài liệu Java
» 500 game java cho nokia đây
» 7 bài tập về java Have Full
» 653 Game Java Cho Nokia ne`!
» Update Tài liệu Java
» 500 game java cho nokia đây
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|