服务端
/** 导包 */
public class ServerDemo{
public static void main(String[] args) throws Exception{
//1.创建服务,监听一个窗口
ServerSocket ss = new ServerSocket(4372);
Socket s = ss.accept();
//输入流
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
public static void main(String[] args) throws Exception{
//1.创建服务,监听一个窗口
ServerSocket ss = new ServerSocket(4372);
Socket s = ss.accept();
//输入流
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
while(true) {
String msg = dis.readUTF();
System.out.println("服务器已收到!" +msg);
//输出流
dos.writeUTF( msg);
dos.flush();
}
String msg = dis.readUTF();
System.out.println("服务器已收到!" +msg);
//输出流
dos.writeUTF( msg);
dos.flush();
}
}
}
客户端
/** 导包 */
public class ClientDemo {
public static void main(String[] args) throws UnknownHostException, IOException { Socket s = new Socket("localhost", 4372);
new Thread(new SendDemo(s)).start();
new Thread(new ReceDemo(s)).start();
}
}
new Thread(new ReceDemo(s)).start();
}
}
/**
* 接受类
*/
/** 导包 */
public class ReceiveDemo implements Runnable{
//输入流
private DataInputStream dis;
//线程标识
private boolean isRunning = true;
private DataInputStream dis;
//线程标识
private boolean isRunning = true;
public ReceiveDemo() {
}
public ReceiveDemo(Socket socket) {
try {
dis = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
}
}
public ReceiveDemo(Socket socket) {
try {
dis = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
}
/**
* 接受数据方法
* @return
*/
public String receive() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
return msg;
}
@Override
public void run() {
while (isRunning) {
receive();
}
}
}
* 接受数据方法
* @return
*/
public String receive() {
String msg = "";
try {
msg = dis.readUTF();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dis);
}
return msg;
}
@Override
public void run() {
while (isRunning) {
receive();
}
}
}
/**
* 发送类
*/
/** 导包 */
public class SendDemo implements Runnable {
//控制台输入流
private BufferedReader br ;
//管道输出流
private DataOutputStream dos;
//线程是否正常运行,默认为true
private boolean isRunning = true;
public SendDemo (){
br = new BufferedReader(new InputStreamReader(System.in));
}
public SendDemo (Socket s) {
this();
try {
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos,br);
}
}
/**
* 从控制台接受数据
*/
private String getMsgFromConsle() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 输出数据
*/
private void sendMsg() {
String msg = getMsgFromConsle();
if(msg != null && !msg.equals("")) {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos,br);
}
}
}
@Override
public void run() {
while(isRunning) {
sendMsg();
}
}
}
private BufferedReader br ;
//管道输出流
private DataOutputStream dos;
//线程是否正常运行,默认为true
private boolean isRunning = true;
public SendDemo (){
br = new BufferedReader(new InputStreamReader(System.in));
}
public SendDemo (Socket s) {
this();
try {
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos,br);
}
}
/**
* 从控制台接受数据
*/
private String getMsgFromConsle() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 输出数据
*/
private void sendMsg() {
String msg = getMsgFromConsle();
if(msg != null && !msg.equals("")) {
try {
dos.writeUTF(msg);
dos.flush();
} catch (IOException e) {
e.printStackTrace();
isRunning = false;
CloseUtil.closeAll(dos,br);
}
}
}
@Override
public void run() {
while(isRunning) {
sendMsg();
}
}
}