Java面试笔试题及答案汇总终结篇:代码与编程

来源:www.hxw.red  作者:本站整理
摘要:关于java代码与编程的综合应用135、继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?答:父类:package test;public class FatherClass public FatherClass() Syste……

138、Java 的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示?

答:Server端程序:

package test;
import java.net.*;
import java.io.*;

public class Server{
  private ServerSocket ss;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  public Server() {
  try {
   ss=new ServerSocket(10000);
   while(true) {
  socket = ss.accept();
  String RemoteIP = socket.getInetAddress().getHostAddress();
  String RemotePort = ":"+socket.getLocalPort();
  System.out.println("A client come in!IP:"+RemoteIP+RemotePort);
  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  String line = in.readLine();
  System.out.println("Cleint send is :" + line);
  out = new PrintWriter(socket.getOutputStream(),true);
  out.println("Your Message Received!");
  out.close();
  in.close();
  socket.close();
   }
  }catch (IOException e) {
   out.println("wrong");
  }
 }
 public static void main(String[] args) {
  new Server();
 }
}

Client端程序:

package test;
import java.io.*;
import java.net.*;

public class Client {
 Socket socket;
 BufferedReader in;
 PrintWriter out;
 public Client() {
  try {
   System.out.println("Try to Connect to 127.0.0.1:10000");
   socket = new Socket("127.0.0.1",10000);
   System.out.println("The Server Connected!");
   System.out.println("Please enter some Character:");
   BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
   out = new PrintWriter(socket.getOutputStream(),true);
   out.println(line.readLine());
   in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
   System.out.println(in.readLine());
   out.close();
   in.close();
   socket.close();
  }catch(IOException e) {
   out.println("Wrong");
  }
 }
 public static void main(String[] args) {
  new Client();
 }
}

【相关文章】好搜一下
揭秘金山网盾破钓鱼骗局的四大核心技术

揭秘金山网盾破钓鱼骗局的四大核心技术

一款大小不到2M,安装后占用系统资源仅为1M的免费软件,金山网盾在推出一周后,安…