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 7920687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:18:40+00:00 2026-06-03T16:18:40+00:00

I tried to implement a send-receive example via a socket but It didn’t work

  • 0

I tried to implement a send-receive example via a socket but It didn’t work well. The sender sends the data successfully and the Receiver receives the data and shown in the console but I want to save this data in a file and I couldn’t. As I noticed that the receiver keeps listeninig without ending the while loop. So can anyone help me to solve this problem ?

The Sender module

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


public class UDPSend {


 public static void main(String[] args) throws IOException
  {
      FileInputStream fstream = new FileInputStream("T.txt");
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));

      File file = new File("T.txt");
      FileInputStream fis = new FileInputStream(file);
      byte[] fsize = new byte[(int) file.length()];
      int size = fis.read(fsize);
                System.out.println("Size = " + size);



      InetAddress addr = InetAddress.getByName("localhost");
      byte[]  buf  = new byte[10000];

      String DataLine; 
      while ((DataLine = br.readLine()) != null)  
      { 
                DatagramPacket packet =new DatagramPacket(DataLine.getBytes(),         DataLine.length() , addr, 4555);
          System.out.println (DataLine);
          DatagramSocket socket = new DatagramSocket();
          socket.send(packet);

      }//end while loop


  }//end main method 

 }

The Receiver module

import java.io.*;
import java.net.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class UDPRecieve {



public static void main(String args[]) throws Exception
      {

      FileWriter fw = new FileWriter(new File("D:/JavaPrograms/Multimedia-proj-2/src/outtt.txt"));
      fw.write("hi");
      try{
      //DatagramSocket serverSocket = new DatagramSocket(4555);
      DatagramSocket Socket = new DatagramSocket(4555);
      byte[] receiveData = new byte[1000000];    
     // byte[] sendData = new byte[1024];
      //while(true)
      while(receiveData !=null)
         {  
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            Socket.receive(receivePacket);
            String sentence = new String( receivePacket.getData());
            fw.write(sentence);
            fw.flush();
            System.out.printf("RECEIVED: %s " , new String(receivePacket.getData()));
            //System.out.println("Done");
            //InetAddress IPAddress = receivePacket.getAddress();
            //int port = receivePacket.getPort();
            //String capitalizedSentence = sentence.toUpperCase();
           /* sendData = capitalizedSentence.getBytes();
            DatagramPacket sendPacket =
            new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);*/
         }


      fw.flush();
      fw.close(); 

      } catch (Exception e) {
          System.err.println(e);
        }

    }
}

Thanks in advance.

  • 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-06-03T16:18:44+00:00Added an answer on June 3, 2026 at 4:18 pm

    You can achive it by following code changes.In Receiver class make changes in following loop.

    while (receiveData != null) {
                    DatagramPacket receivePacket = new DatagramPacket(receiveData,
                            receiveData.length);
                    Socket.receive(receivePacket);
                    String sentence = new String(receivePacket.getData());
                    fw.write(sentence.trim());
                    fw.flush();
                    System.out.printf("RECEIVED: %s ", new String(receivePacket
                            .getData()));
                    // System.out.println("Done");
                    // InetAddress IPAddress = receivePacket.getAddress();
                    // int port = receivePacket.getPort();
                    // String capitalizedSentence = sentence.toUpperCase();
                    /*
                     * sendData = capitalizedSentence.getBytes(); DatagramPacket
                     * sendPacket = new DatagramPacket(sendData, sendData.length,
                     * IPAddress, port); serverSocket.send(sendPacket);
                     */
                }
    

    EDIT

    Complete Code of the Program which is running successfully.

    Sender

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    
    public class UDPSend {
    
    
     public static void main(String[] args) throws IOException
      {
          FileInputStream fstream = new FileInputStream("D:/T.txt");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
          File file = new File("D:/T.txt");
          FileInputStream fis = new FileInputStream(file);
          byte[] fsize = new byte[(int) file.length()];
          int size = fis.read(fsize);
                    System.out.println("Size = " + size);
    
    
    
          InetAddress addr = InetAddress.getByName("localhost");
          byte[]  buf  = new byte[10000];
    
          String DataLine; 
          while ((DataLine = br.readLine()) != null)  
          { 
                    DatagramPacket packet =new DatagramPacket(DataLine.getBytes(),         DataLine.length() , addr, 4555);
              System.out.println (DataLine);
              DatagramSocket socket = new DatagramSocket();
              socket.send(packet);
    
          }//end while loop
    
    
      }//end main method 
    
     }
    

    Receiver

    import java.io.File;
    import java.io.FileWriter;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    public class UDPReceive {
    
        public static void main(String args[]) throws Exception {
    
            FileWriter fw = new FileWriter(new File(
                    "D:/outtt.txt"));
            fw.write("hi");
            try {
                // DatagramSocket serverSocket = new DatagramSocket(4555);
                DatagramSocket Socket = new DatagramSocket(4555);
                byte[] receiveData = new byte[1000000];
                // byte[] sendData = new byte[1024];
                // while(true)
                while (receiveData != null) {
                    DatagramPacket receivePacket = new DatagramPacket(receiveData,
                            receiveData.length);
                    Socket.receive(receivePacket);
                    String sentence = new String(receivePacket.getData());
                    fw.write(sentence.trim());
                    fw.flush();
                    System.out.printf("RECEIVED: %s ", new String(receivePacket
                            .getData()));
                    // System.out.println("Done");
                    // InetAddress IPAddress = receivePacket.getAddress();
                    // int port = receivePacket.getPort();
                    // String capitalizedSentence = sentence.toUpperCase();
                    /*
                     * sendData = capitalizedSentence.getBytes(); DatagramPacket
                     * sendPacket = new DatagramPacket(sendData, sendData.length,
                     * IPAddress, port); serverSocket.send(sendPacket);
                     */
                }
    
                fw.flush();
                fw.close();
    
            } catch (Exception e) {
                System.err.println(e);
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to implement __concat__ , but it didn't work >>> class lHolder(): ...
Well, i am at new at Socket programming in java. I tried to implement
I found this API called Kryonet . Well, i tried to implement the example
I have tried to implement css sticky footer on my page but it doesn't
I tried to implement a small authentication via http and copied this bit of
I tried to implement XOR swap in python. x,y= 10,20 x,y,x = x^y,x^y,x^y print('%s
I tried to implement the following in less: nav > ul > li:first-child using:
I tried to implement FizzBuzz in DCPU-16. I use this web emulator: http://mappum.github.com/DCPU-16/ (repository:
I once tried to implement Comet in PHP. Soon, I found that PHP is
I've an WPF application where tried to implement MVVM pattern and Prism 2. I

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.