socket
服务器端代码
package cn.java.net.TCP.infoExchange;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static int count;
private static Socket socket;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9527);
System.out.println("服务器已启动,等待客户连接...");
while(true){
socket = serverSocket.accept();
ServerThread st = new ServerThread(socket);
st.start();
System.out.println("这是第"+(count++)+"位用户");
System.out.println("此用户的IP地址为"+socket.getInetAddress().getHostAddress());
}
}
}
服务器处理函数:
package cn.java.net.TCP.infoExchange;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public class ServerThread extends Thread {
private Socket socket = null;
private InputStream inputStream = null;
private InputStreamReader inputStreamReader = null;
private BufferedReader bufferedReader = null;
private OutputStream outputStream = null;
private PrintStream printStream = null;
private String a;
private String b = "地瓜地瓜,我是土豆";
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
super.run();
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
try {
while((a = bufferedReader.readLine()) != null){
System.out.println("客户端说:"+a);
socket.shutdownInput();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
printStream = new PrintStream(outputStream);
try {
printStream.write(b.getBytes());
socket.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
package cn.java.net.TCP.infoExchange;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Cilent {
private static String info = " 土豆土豆,我是地瓜";
private static String localhost;
private static byte[] b;
private static String a;
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket(localhost, 9527);
OutputStream outputStream = socket.getOutputStream();
PrintStream ps = new PrintStream(outputStream);
b = info.getBytes();
ps.write(b);
ps.flush();
//千万不能忘记关闭输入输出流!!否则不会出结果!!
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
while((a = bf.readLine()) != null){
System.out.println("服务器说:"+a);
}
socket.shutdownInput();
bf.close();
inputStream.close();
ps.close();
outputStream.close();
socket.close();
}
}
注意事项:应先启动服务器端在启动客户端,两者在各自的控制台分别输出对应的打印语句.在Eclipse中按照下图切换:
以上代码参考自
上一篇: 07-NIO SelectionKey
下一篇: socket