How to transfer files between TCP server and TCP client in Java

I have implemented simple TCP server and TCP client classes, which can send messages from the client to the server, and the messages will be converted to uppercase on the server, but how to transfer files from the server to the client and upload files from the client to the server The following code is what I got

TCPClient. java:

import java.io.*;
import java.net.*;

class TCPClient {
 public static void main(String args[]) throws Exception {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(system.in));
        Socket clientSocket = new Socket("127.0.0.1",6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + "\n");
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER:" + modifiedSentence);
        clientSocket.close();
    }
}

TCPServer. java:

import java.io.*;
import java.net.*;

class TCPServer {
    public static void main(String args[]) throws Exception {
        int firsttime = 1;
        while (true) {
            String clientSentence;
            String capitalizedSentence="";
            ServerSocket welcomeSocket = new ServerSocket(3248);
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            //System.out.println(clientSentence);
            if (clientSentence.equals("set")) {
                outToClient.writeBytes("connection is ");
                System.out.println("running here");
                //welcomeSocket.close();
                //outToClient.writeBytes(capitalizedSentence);
            }
            capitalizedSentence = clientSentence.toUpperCase() + "\n";
            //if(!clientSentence.equals("quit"))
            outToClient.writeBytes(capitalizedSentence+"enter the message or command: ");
            System.out.println("passed");
            //outToClient.writeBytes("enter the message or command: ");
            welcomeSocket.close();
            System.out.println("connection terminated");
        }
    }
}

So, first execute tcpserver Java, and then execute tcpclient Java, I try to use tcpserver If clause in Java to test user input. Now I really want to realize how to download and upload from both sides

Solution

This link should help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>