Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:24:38+00:00 2026-05-23T09:24:38+00:00

I want to create small client-server TCP file transfer program. And I have problem

  • 0

I want to create small client-server TCP file transfer program. And I have problem with one thing. When I send file from Client to Server, for example a txt file: omg.txt, I want the Server to read the incoming file name.

So – Client send omg.txt, Server says “New file recived: omg.txt”. I tried to use BufferedReader (Server) and DataOutputStream (Client, because you have to write name of the file to send it) but it didnt work.

EDIT:

Client:

Socket sock = new Socket("localhost",13267);
System.out.println("Wait...");
Scanner input = new Scanner(System.in);

while(true){
// wysylanie
System.out.println("Filename");
String p = input.nextLine();
File myFile = new File (p);
boolean exists = (new File(p)).exists();
if (exists) {
        byte [] mybytearray  = new byte [(int)myFile.length()];
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());
        bis.read(mybytearray,0,mybytearray.length);
        OutputStream os = sock.getOutputStream();
        System.out.println("Wait...");
        os.write(mybytearray,0,mybytearray.length);
        os.flush();
        System.out.println("Done");
        sock.close();
    } 

Server:

int filesize=6022386;
int bytesRead;
int current = 0;

ServerSocket servsock = new ServerSocket(13267);
Scanner input = new Scanner(System.in);


while (true) {
System.out.println("Wait...");
Socket sock = servsock.accept();
System.out.println("OK : " + sock);

// odbior pliku
byte [] mybytearray  = new byte [filesize];
InputStream is = sock.getInputStream();
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
   bytesRead =
      is.read(mybytearray, current, (mybytearray.length-current));
   if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

System.out.println("New filename");
String p = input.nextLine();
File plik = new File(p);
FileOutputStream fos = new FileOutputStream(plik);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(mybytearray, 0 , current);
bos.flush();

System.out.println("File saved");
bos.close();
sock.close();

And I tried to do something like that:

Client addon:

DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream());

sentence = input.nextLine();
outToServer.writeBytes(sentence);

Server addon:

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(sock.getInputStream()));

clientSentence = inFromClient.readLine();
System.out.println(clientSentence);

…but unfortunetly I have “Wait..” all the time for Client

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:24:39+00:00Added an answer on May 23, 2026 at 9:24 am

    TCP just transfers raw data. If you want to send files with a filename, you may want to use a higher level protocol, such as FTP or TFTP.

    If you really want to use plain TCP, you’ll need to encode the filename in your message somehow. You could, for example, send the filename as the first line of the message, and then have the other end turn the rest of the message into a file with that name.

    This may be helpful to you: http://www.adp-gmbh.ch/blog/2004/november/15.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a small java application to copy some wiki content from
I want to create a small program in C# in Windows that would open
I need to implement a TCP client application. The client and the server send
I want to globalize my application. I have created a small form which asks
I want a small (< 30MB) standalone Windows executable (a single file) that creates
I want to create a client side mail creator web page. I know the
I want to create a simple http proxy server that does some very basic
I want to create my small html editor with using html5 <section contenteditable=true> Edit
I'm working on a client-side Java application for which I want to create a
I want to create a small delay so that my 1st set of codes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.