Tomcat框架对于新手不好安装,并且我们只是想要一个能够返回数据的服务器而已,所以直接通过Socket实现Http是非常简单的!

服务端源码

package larson;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


public class Main {
    
    /**
     * @throws Exception
     */
    public void makeConn(String ip, int port) throws Exception {
        Socket client = new Socket(ip, port);
        try {
//            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//            BufferedReader input = new BufferedReader(new InputStreamReader(new ByteArrayInputStream("客户端发给服务器端的信息".getBytes())));
            BufferedInputStream in = new BufferedInputStream(client.getInputStream());
            PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));//这里设置输出编码
            
            String clientString = null;
            String serverString = null;
            while (true) {
                byte[] buf = new byte[10000];
                int size=0;
                if ((size=in.read(buf))!=-1) {
                    serverString = new String(buf,0,size,"utf-8");//这里设置读取编码
                    if (serverString != null) {
                        System.out.println("server:" + serverString);
                    }
                }
//                clientString = input.readLine();
                clientString = "客户端发给服务器端的信息";
                if (clientString != null) {
                    out.println(clientString);
                    out.flush();
                    break;
                }
            }
            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
    }
  
    
    public static void main(String[] args) {
        Main sc =new Main();
        int port = 48264;
        try {
            sc.makeConn("127.0.0.1", port);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

客户端源码

package larson;
import java.io.BufferedInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;


public class Main {
    
    /**
     * @throws Exception
     */
    public void makeConn(String ip, int port) throws Exception {
        Socket client = new Socket(ip, port);
        try {
//            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
//            BufferedReader input = new BufferedReader(new InputStreamReader(new ByteArrayInputStream("客户端发给服务器端的信息".getBytes())));
            BufferedInputStream in = new BufferedInputStream(client.getInputStream());
            PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));//这里设置输出编码
            
            String clientString = null;
            String serverString = null;
            while (true) {
                byte[] buf = new byte[10000];
                int size=0;
                if ((size=in.read(buf))!=-1) {
                    serverString = new String(buf,0,size,"utf-8");//这里设置读取编码
                 // System.out.println( bytesToHexString(buf));
                    if (serverString != null) {
                        System.out.println("server:" + serverString);
                    }
                }
//                clientString = input.readLine();
                clientString = "客户端发给服务器端的信息";
                if (clientString != null) {
                    out.println(clientString);
                    out.flush();
                    break;
                }
            }
            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
    }
      
    
    public static void main(String[] args) {
        Main sc =new Main();
        int port = 48264;
        try {
            sc.makeConn("127.0.0.1", port);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

生成jar包

参考教程:http://blog.csdn.net/dongsheng186/article/details/46635915

上传jar包

参考教程:http://blog.csdn.net/qq_26914291/article/details/78517396

在xshell5执行jar包

java -jar xxx.jar

客户端请求

server:欢迎客户端连入

打印字符乱码问题

在上面代码中统一设置为utf-8编码

java -jar执行不能加载主类问题

参考教程:http://blog.csdn.net/dongsheng186/article/details/46635915
1.注:Main-Class:xxx.xxx 后一定要有回车!

待解决问题

1.虽然客户端显示正常了,但服务端字符依然乱码?(不过不影响使用了)

参考

1.Java中使用Socket实现服务器端和客户端通讯
2.java socket通讯乱码问题的解决

标签: none

仅有一条评论

  1. S先生 S先生

    你好,请问如何把jar上传到服务器,能否再讲详细一点!

添加新评论