package a3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
String hostName = s.getInetAddress().getHostName();
System.out.println(hostName + "connect...");
//输入流,源头:客户端
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//输出流,目的:客户端
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line = null;
while((line =br.readLine()) != null){
System.out.println( "客户端信息:" + line);
String upperCase = line.toUpperCase();
bw.write(upperCase);
bw.newLine();
bw.flush();
}
s.close();
ss.close();
}
}
*********************************************************************************************************************************
package a3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket(InetAddress.getLocalHost(), 8888);
//读取键盘的流,源头:键盘
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输出流,目的:Server
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//输入流,源头:Server
BufferedReader brr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((br.readLine()) != null){
if(line.equals("over")){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
String readLine = brr.readLine();
System.out.println(readLine);
}
br.close();
s.close();
}
}
到底哪里错了,键盘输入后,服务器没反应。
|
|