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;
/**
* 建立一个客户端程序
* 连接服务端
* 从键盘输入接收一个字符串
* 发送到服务端
* 读取服务端发回的字符串
* 打印
* @throws IOException
* @throws UnknownHostException
*/
public class Demo7 {
public static void main(String[] args) throws UnknownHostException, Exception {
Socket socket = new Socket("127.0.0.1",12345);
BufferedReader br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
Scanner sc = new Scanner(System.in);
String string=sc.nextLine();
System.out.println("请输入:");
ps.println(string);
System.out.println(br.readLine());
socket.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo8 {
public static void main(String[] args) throws IOException {
ServerSocket ss= new ServerSocket(12345);
while(true){
final Socket socket= ss.accept();
String ip= socket.getInetAddress().getCanonicalHostName();
System.out.println(ip+"请求连接,连接成功");
new Thread(){
public void run (){
try {
BufferedReader br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
String string=br.readLine();
StringBuffer sb= new StringBuffer(string);
String newstring= sb.reverse().toString();
ps.println(newstring);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
}
我这个一运行就死机,怎么回事 , 求高手指教。 |
|