应用简单的socket,来达到两台机器的对话。这里提供的是访问server端并按你的要求(名字),索要server服务端中数据库中对应名字的电话。还有一个就是返回自身的IP地址。(数据库内容略)
对于server端的代码:
 import java.io.DataInputStream;
import java.io.DataInputStream; import java.io.InputStream;
import java.io.InputStream; import java.io.OutputStream;
import java.io.OutputStream; import java.io.PrintStream;
import java.io.PrintStream; import java.net.ServerSocket;
import java.net.ServerSocket; import java.net.Socket;
import java.net.Socket;

 public class Myserver ...{
public class Myserver ...{
 private final static String PRE_NAME = "Name:";
    private final static String PRE_NAME = "Name:";

 @SuppressWarnings( ...{ "deprecation" })
    @SuppressWarnings( ...{ "deprecation" })
 public static void main(String args[]) ...{
    public static void main(String args[]) ...{
 ServerSocket server;
        ServerSocket server;
 Socket socket;
        Socket socket;
 String s;
        String s;
 InputStream Is;
        InputStream Is;
 OutputStream Os;
        OutputStream Os;
 DataInputStream DIS;
        DataInputStream DIS;
 PrintStream PS;
        PrintStream PS;

 try ...{
        try ...{
 // 在端口4321注册服务
            // 在端口4321注册服务
 server = new ServerSocket(9000);
            server = new ServerSocket(9000);
 // 监听窗口,等待连接
            // 监听窗口,等待连接
 System.out.println("******************************");
            System.out.println("******************************"); System.out.println("*          server ok         *");
            System.out.println("*          server ok         *"); System.out.println("******************************");
            System.out.println("******************************");
 socket = server.accept();
            socket = server.accept();
 // 获得对应Socket的输入/输出流
            // 获得对应Socket的输入/输出流
 Is = socket.getInputStream();
            Is = socket.getInputStream();
 Os = socket.getOutputStream();
            Os = socket.getOutputStream();
 // 建立数据流
            // 建立数据流
 DIS = new DataInputStream(Is);
            DIS = new DataInputStream(Is);
 PS = new PrintStream(Os);
            PS = new PrintStream(Os);
 DataInputStream in = new DataInputStream(System.in);
            DataInputStream in = new DataInputStream(System.in);

 while (true) ...{
            while (true) ...{
 System.out.println("");
                System.out.println("");
 System.out.println("please wait client's message...");
                System.out.println("please wait client's message...");
 System.out.println("");
                System.out.println("");
 s = DIS.readLine(); // 读入从client传来的字符串
                s = DIS.readLine(); // 读入从client传来的字符串
 System.out.println("client said:" + s); // 打印字符串
                System.out.println("client said:" + s); // 打印字符串
 // if (s.trim().equals("BYE"))
                // if (s.trim().equals("BYE")) // break; // 如果是"BYE",就退出
                // break; // 如果是"BYE",就退出

 if (s.toUpperCase().trim().equals("MyAd".toUpperCase())) ...{
                if (s.toUpperCase().trim().equals("MyAd".toUpperCase())) ...{ s = socket.getRemoteSocketAddress().toString();
                    s = socket.getRemoteSocketAddress().toString(); }
                } 
 else if (s.toUpperCase().trim().startsWith(PRE_NAME.toUpperCase())) ...{
                else if (s.toUpperCase().trim().startsWith(PRE_NAME.toUpperCase())) ...{ // 调用数据库查询Address函数
                    // 调用数据库查询Address函数
 if (s.length() > PRE_NAME.length()) ...{
                    if (s.length() > PRE_NAME.length()) ...{ String name = s.substring(PRE_NAME.length());
                        String name = s.substring(PRE_NAME.length()); 
                         name = new String(name.getBytes("UTF-8")); /// 汉字要求!~
                        name = new String(name.getBytes("UTF-8")); /// 汉字要求!~

 if (!"".equals(name.trim())) ...{
                        if (!"".equals(name.trim())) ...{ 
                                                         s = String.valueOf(Daoconnect.getAdressbyName(name.trim()));
                            s = String.valueOf(Daoconnect.getAdressbyName(name.trim())); }
                        } }
                    } }
                } 
 else ...{
                else ...{
 System.out.print("you say:");
                    System.out.print("you say:"); s = in.readLine();
                    s = in.readLine(); }
                }
 PS.println(s); // 将读取得字符串传给client
                PS.println(s); // 将读取得字符串传给client
 if (s.trim().equals("BYE"))
                if (s.trim().equals("BYE")) break; // 如果是"BYE",就退出
                    break; // 如果是"BYE",就退出
 }
            }
 // 关闭连接
            // 关闭连接
 DIS.close(); // 关闭数据输入流
            DIS.close(); // 关闭数据输入流
 PS.close(); // 关闭数据输出流
            PS.close(); // 关闭数据输出流
 Is.close(); // 关闭输入流
            Is.close(); // 关闭输入流
 Os.close(); // 关闭输出流
            Os.close(); // 关闭输出流
 socket.close(); // 关闭sockey
            socket.close(); // 关闭sockey
 }
        }

 catch (Exception e) ...{
        catch (Exception e) ...{
 System.out.println("Error:" + e);
            System.out.println("Error:" + e);
 }
        }
 }
    }
 }
}client端的代码:
 import java.net.*;
import java.net.*;
 import java.io.*;
import java.io.*;

 public class Myclient ...{
public class Myclient ...{
 private final static String HOSTADRESS= "192.168.1.100";
    private final static String HOSTADRESS= "192.168.1.100"; 
    
 @SuppressWarnings(...{"deprecation"})
    @SuppressWarnings(...{"deprecation"})
 public static void main(String args[]) ...{
    public static void main(String args[]) ...{ 
         //判断主机地址有没有
        //判断主机地址有没有 System.out.println("***??");
        System.out.println("***??");
 if ("".equals(HOSTADRESS) || HOSTADRESS == null) ...{
        if ("".equals(HOSTADRESS) || HOSTADRESS == null) ...{ 
 System.out.println("you forget the name of the server!");
            System.out.println("you forget the name of the server!");
 System.exit(1);
            System.exit(1); }
        } 
         Socket socket;
        Socket socket;
 String s = "";
        String s = "";
 InputStream Is;
        InputStream Is;
 OutputStream Os;
        OutputStream Os;
 DataInputStream DIS;
        DataInputStream DIS;
 PrintStream PS;
        PrintStream PS;

 try ...{
        try ...{ // 注意端口号要与服务器保持一致
            // 注意端口号要与服务器保持一致 socket = new Socket(HOSTADRESS, 9000);
            socket = new Socket(HOSTADRESS, 9000);
 System.out.println("*********************************");
            System.out.println("*********************************"); System.out.println("*           client ok           *");
            System.out.println("*           client ok           *"); System.out.println("*********************************");
            System.out.println("*********************************");
 // 获得对应socket的输入/输出流
            // 获得对应socket的输入/输出流
 Is = socket.getInputStream();
            Is = socket.getInputStream();
 Os = socket.getOutputStream();
            Os = socket.getOutputStream();
 // 建立数据流
            // 建立数据流
 DIS = new DataInputStream(Is);
            DIS = new DataInputStream(Is);
 PS = new PrintStream(Os);
            PS = new PrintStream(Os);
 DataInputStream in = new DataInputStream(System.in);
            DataInputStream in = new DataInputStream(System.in);

 while (true) ...{
            while (true) ...{
 System.out.print("you say:");
                System.out.print("you say:");
 s = in.readLine(); // 读取用户输入的字符串
                s = in.readLine(); // 读取用户输入的字符串
 PS.println(new String(s.getBytes("UTF-8"))); // 将读取得字符串传给server
                PS.println(new String(s.getBytes("UTF-8"))); // 将读取得字符串传给server
 if (s.trim().equals("BYE"))
                if (s.trim().equals("BYE")) break; // 如果是"BYE",就退出
                    break; // 如果是"BYE",就退出
 else
                else

 ...{
                ...{
 System.out.println("");
                    System.out.println("");
 System.out.println("please wait server's message...");
                    System.out.println("please wait server's message...");
 System.out.println("");
                    System.out.println("");
 }
                }
 s = DIS.readLine(); // 从服务器获得字符串
                s = DIS.readLine(); // 从服务器获得字符串
 System.out.println("server said:" + s); // 打印字符串
                System.out.println("server said:" + s); // 打印字符串
 if (s.trim().equals("BYE"))
                if (s.trim().equals("BYE")) break; // 如果是"BYE",就退出
                    break; // 如果是"BYE",就退出
 }
            }
 // 关闭连接
            // 关闭连接
 DIS.close(); // 关闭数据输入流
            DIS.close(); // 关闭数据输入流
 PS.close(); // 关闭数据输出流
            PS.close(); // 关闭数据输出流
 Is.close(); // 关闭输入流
            Is.close(); // 关闭输入流
 Os.close(); // 关闭输出流
            Os.close(); // 关闭输出流
 socket.close(); // 关闭socket
            socket.close(); // 关闭socket
 }
        }

 catch (Exception e) ...{
        catch (Exception e) ...{
 System.out.println("Error:" + e);
            System.out.println("Error:" + e);
 }
        }
 }
    }
 }
}
注意 以上实验的填自己真实的IP地址。
此代码已通过实验是可行的,有问题可以找我联系。
 
                   
                   
                   
                   
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1333
					1333
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            