TCP服务器端程序代码如下:
public class server {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
BufferedReader in=null;
PrintWriter out=null;
try {
//设置服务器的端口号
server=new ServerSocket(9000);
//接收客户端连接
socket=server.accept();
//得到客户端输入信息
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//向客户端输入信息,true是自动刷新缓冲区
out = new PrintWriter(socket.getOutputStream(),true);
out.println("已建立与服务器的连接");
String info = in.readLine();
System.out.println("客户端输入的是:"+info);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
out.close();
socket.close();
server.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class clientSocket {
/**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
BufferedReader in=null;
BufferedReader input=null;
PrintWriter out=null;
try {
socket=new Socket("127.0.0.1",9000);
while(true){
input=new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream(),true);
out.println("客户端说:"+input.readLine());
System.out.println("服务器说:"+in.readLine());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class server {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
BufferedReader in=null;
PrintWriter out=null;
try {
//设置服务器的端口号
server=new ServerSocket(9000);
//接收客户端连接
socket=server.accept();
//得到客户端输入信息
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//向客户端输入信息,true是自动刷新缓冲区
out = new PrintWriter(socket.getOutputStream(),true);
out.println("已建立与服务器的连接");
String info = in.readLine();
System.out.println("客户端输入的是:"+info);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
out.close();
socket.close();
server.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class clientSocket {
/**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
BufferedReader in=null;
BufferedReader input=null;
PrintWriter out=null;
try {
socket=new Socket("127.0.0.1",9000);
while(true){
input=new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream(),true);
out.println("客户端说:"+input.readLine());
System.out.println("服务器说:"+in.readLine());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}