java S/C的通信基础(双向传输)
服务器<——————>客户端(双向传输)
//服务器端
package servertoclient.twoline;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerToClientTwoLine {
/**
* @param args
*/
public static void main(String[] args) {
ServerSocket server = null;
PrintStream out = null;
BufferedReader in=null;
try {
server=new ServerSocket(38002);
Socket s=server.accept();
System.out.println("Server OK");
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintStream(s.getOutputStream(),true);
while(true)
{
/*
* 输入到服务器端的数据
*/
String inString=in.readLine();
System.out.println("Client说:"+inString);
/*
* 输出到客户端的数据
*/
System.out.println("请输入要发送到客户端的数据:");
Scanner scanner=new Scanner(System.in);
String line=scanner.next();
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally
{
try {
in.close();
out.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerToClientTwoLine {
/**
* @param args
*/
public static void main(String[] args) {
ServerSocket server = null;
PrintStream out = null;
BufferedReader in=null;
try {
server=new ServerSocket(38002);
Socket s=server.accept();
System.out.println("Server OK");
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintStream(s.getOutputStream(),true);
while(true)
{
/*
* 输入到服务器端的数据
*/
String inString=in.readLine();
System.out.println("Client说:"+inString);
/*
* 输出到客户端的数据
*/
System.out.println("请输入要发送到客户端的数据:");
Scanner scanner=new Scanner(System.in);
String line=scanner.next();
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally
{
try {
in.close();
out.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//客户端
package servertoclient.twoline;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
BufferedReader in=null;
PrintStream out=null;
try {
socket=new Socket("127.0.0.1",38002);
out=new PrintStream(socket.getOutputStream(),true);
in=new BufferedReader (new InputStreamReader(socket.getInputStream()));
while(true)
{
/*
* 发送到服务器的数据
*/
System.out.println("请输入发送到服务器的数据:");
//不断读取键盘的输入
Scanner scanner=new Scanner(System.in);
String line= scanner.next();
out.println(line);
/*
* 接收服务器的数据
*/
String instring=in.readLine();
System.out.println("Server说:"+instring);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
Socket socket=null;
BufferedReader in=null;
PrintStream out=null;
try {
socket=new Socket("127.0.0.1",38002);
out=new PrintStream(socket.getOutputStream(),true);
in=new BufferedReader (new InputStreamReader(socket.getInputStream()));
while(true)
{
/*
* 发送到服务器的数据
*/
System.out.println("请输入发送到服务器的数据:");
//不断读取键盘的输入
Scanner scanner=new Scanner(System.in);
String line= scanner.next();
out.println(line);
/*
* 接收服务器的数据
*/
String instring=in.readLine();
System.out.println("Server说:"+instring);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}