资源预览内容
亲,该文档总共2页全部预览完了,如果喜欢就下载吧!
资源描述
客户端代码:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;import java.net.UnknownHostException;public class LoginClient public static void main(String args) / TODO Auto-generated method stubtry Socket socket = new Socket(localhost,8800);InputStream is = socket.getInputStream();OutputStream os = socket.getOutputStream();String info = 用户名:Tom;用户密码:123456;os.write(info.getBytes();socket.shutdownOutput();String reply =null;BufferedReader br = new BufferedReader(new InputStreamReader(is);while(!(reply = br.readLine() = null)System.out.println(我是客户端,服务器响应为:+reply);br.close();os.close();is.close();socket.close(); catch (UnknownHostException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();服务器端代码:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;public class LoginServer /* * param args */public static void main(String args) / TODO Auto-generated method stubtry ServerSocket serverSocket = new ServerSocket(8800);while(true)Socket socket = serverSocket.accept();InputStream is = socket.getInputStream();OutputStream os = socket.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is);String info = null;while(!(info=br.readLine()=null)System.out.println(我是服务器,用户信息为:+info);String reply = 欢迎你,登陆成功!;os.write(reply.getBytes();br.close();os.close();is.close();socket.close();serverSocket.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号