Java 的通信编程,编程题(或问答),用 JAVA SOCKET 编程, 读服务器几个字符,再写入本地显示?
Server端
public class Server {
public static void main (String [] args){
new Server();
}
public Server(){
ServerSocket ss = null ;
Socket soc;
BufferedReader in = null;
try {
ss=new ServerSocket(12345);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true)
try {
soc = ss.accept();
System.out.println("有人进来了 ip为:"+soc.getInetAddress().getHostAddress()+" : "+soc.getPort());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
System.out.println("Client send is "+in.readLine());
} catch (IOException e) {
if(ss!=null){
try {
System.out.println("服务端错误处理");
ss.close();
} catch (IOException e2) {
}
}
// TODO Auto-generated catch block
System.out.print("错误是"+e.getLocalizedMessage());
e.printStackTrace();
}
}
}
Client端
public class Client {
Socket socket;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client();
}
public Client(){
PrintWriter out = null;
BufferedReader in =null;
try {
socket = new Socket("127.0.0.1",12345);
System.out.println("Server Connect Success");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
try {
System.out.println("打字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(socket.getOutputStream(),true);
out.println(br.readLine());
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("client in :" + in.readLine());
} catch (Exception e) {
try {
System.out.println("Client is wrong");
socket.close();
out.close();
in.close();
} catch (Exception e1e) {
}
}
}
}
}
Server端
public class Server {
public static void main (String [] args){
new Server();
}
public Server(){
ServerSocket ss = null ;
Socket soc;
BufferedReader in = null;
try {
ss=new ServerSocket(12345);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true)
try {
soc = ss.accept();
System.out.println("有人进来了 ip为:"+soc.getInetAddress().getHostAddress()+" : "+soc.getPort());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
System.out.println("Client send is "+in.readLine());
} catch (IOException e) {
if(ss!=null){
try {
System.out.println("服务端错误处理");
ss.close();
} catch (IOException e2) {
}
}
// TODO Auto-generated catch block
System.out.print("错误是"+e.getLocalizedMessage());
e.printStackTrace();
}
}
}
Client端
public class Client {
Socket socket;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client();
}
public Client(){
PrintWriter out = null;
BufferedReader in =null;
try {
socket = new Socket("127.0.0.1",12345);
System.out.println("Server Connect Success");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
try {
System.out.println("打字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(socket.getOutputStream(),true);
out.println(br.readLine());
in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("client in :" + in.readLine());
} catch (Exception e) {
try {
System.out.println("Client is wrong");
socket.close();
out.close();
in.close();
} catch (Exception e1e) {
}
}
}
}
}
本文提供了一个Java Socket编程的基础示例,包括客户端和服务端的实现方式。服务端监听特定端口接收客户端消息并打印,客户端连接到服务端并发送输入的消息。
3378

被折叠的 条评论
为什么被折叠?



