慕前端0336099
2016-03-30 10:51
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class Client {
public static void main(String[] args) {
try {
//创建客户端Socket 指定服务器端地址和URL端口
Socket kos=new Socket("localhost",8990);
//客户端向服务器端发送登录信息 获取输出流
OutputStream am= kos.getOutputStream();
//字节流换成打印流
PrintWriter cm=new PrintWriter(am);
cm.write("用户名:admin;密码:123456");
cm.flush();//使用flush方法向服务端发送输出
kos.shutdownOutput();;//关闭输出流
//关闭资源
cm.close();
am.close();
kos.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
没输出能有什么反应啊……
package Soc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* 客户端
*/
public static void main(String[] args) {
try {
Socket socket =new Socket("localhost",8888);
OutputStream os=socket.getOutputStream(); //字节输出流
PrintWriter pw=new PrintWriter(os); //将输出流包装为打印流
pw.write("用户名:hehe ;密码:456");
pw.flush(); //刷新缓存
socket.shutdownOutput(); //关闭输出流
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String info=null;
while((info=br.readLine())!=null){
System.out.println("我是客户端,服务器端说:"+info);
}
socket.shutdownInput();
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java Socket应用---通信是这样练成的
125012 学习 · 590 问题
相似问题