How to send data in a java socket program without closing socket
NickName:BlackPOP Ask DateTime:2015-03-11T15:13:44

How to send data in a java socket program without closing socket

i created a sever socket program to send a stream data to Apache spark.But data is received by spark after i close the socket or termination of program.i need to send data without closing socket and terminating program.

 import java.io.DataOutputStream;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.util.Scanner;

public class SocketServer {
public static void main(String[] args) {
    try {
        ServerSocket ss = new ServerSocket(9999);
        Socket s = ss.accept();// establishes connection

        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
        Scanner scanner = new Scanner(System.in);
        String s1 = "";
        while (!s1.equals("end")) {
            s1 = scanner.next();
            dout.writeUTF(s1);
            dout.flush();
        }
     ss.close();
    } catch (Exception e) {r
        System.out.println(e);
    }
  }
}

i can send data in stream using nc server nc -lk 9999.

EDIT -1 Tried with println

try {
        ServerSocket ss = new ServerSocket(6000);
        Socket s = ss.accept();// establishes connection

        OutputStream ostream = s.getOutputStream();
        PrintWriter pwrite = new PrintWriter(ostream, true);
        Scanner scanner = new Scanner(System.in);
        String s1 = "";
        while (!s1.equals("end")) {
            s1 = scanner.next();
            pwrite.println(s1);
            pwrite.flush();
        }
        ss.close();
    } catch (Exception e) {
        System.out.println(e);
    }

Still not working.

Please help..

Copyright Notice:Content Author:「BlackPOP」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28980604/how-to-send-data-in-a-java-socket-program-without-closing-socket

Answers
user207421 2015-03-11T07:28:48

Unless Apache Spark is (a) written in Java and (b) calling readUTF() you're using the wrong method to send. You should probably be using a println() method. You also need to close the accepted socket, as well as the server socket.",


More about “How to send data in a java socket program without closing socket” related questions

How to send data in a java socket program without closing socket

i created a sever socket program to send a stream data to Apache spark.But data is received by spark after i close the socket or termination of program.i need to send data without closing socket and

Show Detail

How to call Socket Send and Receive multiple times without closing the socket?

I'm using the .NET Socket class. Basically my program is to send XML commands via TCP to my server (running some 3rd party services), and it will send back an XML response. What I wanted to do is...

Show Detail

Send String through a socket without closing the socket afterward

I'm new to python and i want to send a string through a socket, I did some research and i go this code import socket import sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('

Show Detail

sending image via socket without closing the socket

I created an application in which the server(desktop) sends data and files via socket to multiple clients on android which are connected and all client socket objects are stored in a hashset. Now ...

Show Detail

Sending and receiving that same data by the same program, over a network socket

I'm trying to send a data packet, and receive that same data packet, by a java program over a socket. The point is to measure the time it takes to go over the network and return to my program itsel...

Show Detail

Send data to an open server socket

I have a socket server program running on a remote machine,I want to send data through a client socket program without creating a new socket connection everytime. As long as the server socket is op...

Show Detail

Send socket before closing

I have this code: public static List<Socket> samp = new List<Socket>(); Socket connection/etc in form load.. IPHostEntry host = null; Socket sock; host = Dns.GetHostEntry(sending ip...

Show Detail

Closing socket on program termination

I have a server/client program that lets clients send requests to the server, my program works fine but I have encountered an issue, I noticed that when I'm testing the program over and over again ...

Show Detail

Java Socket closing OutputStream

I have always learned to always close a stream when I finish using it. Java 7 gives you a new option to do this (namely: try-with-resources), and it's all fine, except that when I close the OutputS...

Show Detail

How to send end of file without closing tcp socket

I am trying to send a file over a tcp socket in a peer to peer chat system coded in python. The receiving socket seems not to know that there is no more file to receive. The only way I can get the

Show Detail