基于TCP和UDP的聊天功能的实现

    这里类似于登陆功能的实现,是基于UDP实现的,而聊天的内容,是基于TCP实现的,大概流程是:

    建立一个服务端:

   下面是UDP开启服务器返回给客户端的确定值:

   public void ServerReceive(){
if(serversocket == null){
try {
serversocket = new DatagramSocket(PORT);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

try {
byte data[] = new byte[1024];
packet= new DatagramPacket(data , data.length);
System.out.println("aaaaaaaaaaaaaaaa");
serversocket.receive(packet);
if(packet != null){
login = new String(packet.getData() , packet.getOffset() , packet.getLength());

address = packet.getAddress().toString();
System.out.println(address);
if(packetAddress.size()>0){
for(int i=0;iif(address.equals(packetAddress.get(i)) == true){
position = i;
}
}
}
if(login.equals("Quit") == true){
packetAddress.remove(position);

}else{
if(position != -1){
}else{
packetAddress.add(address);
}
}

}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public void ServerSend(){
if(UDPsocket == null){
try {
UDPsocket = new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(("Quit").equals(login) != true){
try {
InetAddress serverAddress = InetAddress.getByName(address.substring(1));
byte data1[] = login.getBytes();


DatagramPacket  pack = new DatagramPacket (data1 , data1.length , serverAddress , PORT);
UDPsocket.send(pack);

System.out.println("send over"+ login);

} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


客户端:

public void SendToServer(String str,int port,String Address){
try {
InetAddress serverAddress = InetAddress.getByName(Address);
//System.out.println("aaaaaaaaaa"+socket);
if(socket == null){
System.out.println("xxxxxxxxx"+socket);
socket = new  DatagramSocket();
}
byte data[] = str.getBytes();


DatagramPacket  packet = new DatagramPacket (data , data.length,serverAddress,port);
System.out.println(packet);
socket.send(packet);

socket.close();
socket = null;


} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


class ClientSendThread extends Thread{
public void run(){
SendToServer("Login",PORT,"255.255.255.255");

if(Receivesocket == null){
try {
Receivesocket = new DatagramSocket(PORT);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while(isUDP){
try {

byte data[] = new byte[1024];
DatagramPacket pack = new DatagramPacket(data , data.length);
System.out.println("aaaaaaaaaaa");
Receivesocket.receive(pack);
ServiceAddress = pack.getAddress().toString();
String login = new String(pack.getData() , pack.getOffset() , pack.getLength());
System.out.println(login + "----------" + ServiceAddress);
Message msg = new Message();
msg.what = 0;
Bundle b = new Bundle();
b.putString("TAG", login);
msg.setData(b);
MainActivity.this.handle.sendMessage(msg);

}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


简单的登陆功能就实现了,利用上一张说的TCP的实现,进行内容的传递,即可聊天

你可能感兴趣的:(Android)