DataOutputStream not emptying buffer - java

I am working on a server/client communication program and I am stuck at a problem. When I try to send messages from my client side it won't work properly. After initializing the server, I connect the client and that is successful. When I try to send messages from the client, the server won't receive them. After I close the client connection, the server receives all of the messages I attempted to send earlier. The following class is what I am using:
public class ServerSender extends Thread
{
private DataOutputStream out = new DataOutputStream(socket.getOutputStream());
private Scanner kb = new Scanner(System.in);
public void run()
{
while(true)
{
try
{
out.writeUTF(kb.nextLine());
out.flush();
} catch(IOException e) { System.out.println("error"); }
}
}
}
Any help would be much appreciated, thanks.

DataOutputStream doesn't have a buffer to flush, so your diagnosis is incorrect. However you need to be aware that writeUTF() writes a format that only DataInputStream.readUTF() can read. If you're trying to write lines you have the wrong API: try BufferedWriter.write()/.newLine().

Related

UDP Listener doesn't receive data

I am trying to make a UDP Listener listening to a server and receiving data.
Here is the UDPListener :
public class UDPListener extends AsyncTask<Void, String, Void>{
public UDPConnector udpConnector;
public UDPListener(UDPConnector udpConnector){
byte[] buff;
this.udpConnector = udpConnector;
System.out.println("UDPListener initalized");
}
#Override
protected Void doInBackground(Void... voidResult){
Looper.prepare();
while(true){
byte[] buffer = new byte[512];
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
try {
System.out.println("Message reçu debug ");
udpConnector.UDPSocket.receive(p);
if(p.getData().length > 0){
buffer = p.getData();
String s = new String(buffer, 0, p.getLength());
publishProgress(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
udpConnector.main.handleTextReceived(values[values.length-1]);
}
}
The problem is publishProgress() never gets executed. I am sure that the UDPConnector is well configured because i can send data to the server. And i know that the method doInBackground is executed. I am also sure that the serveur is sending data to me as i can receive it with other tools.
I can post more code if needed, please let me know.
Thanks
If you are using the phone emulator, the emulator might be changing your expected IP address for which you will have to create a redirect.
Look at this post
Datagram (UDP) receiver not working - not receiving broadcast packets
So, you don't initialize the socket with a specific port? which one is your server using for sending the datagrams? Maybe your problem is, that the socket is listening to a random port, which does not match to your port.

How to control the printwriter from a separate method?

Im working on building my own GUI program that talks to my pc from a tablet. I have the server side done in java but my problem is on the client side.
I want to send data out the PrintWriter to the server from a separate method.
I have accomplished sending in the code below (it sends 'a') but i cant figure out how to send it from a separate method. i believe its a basic java scope problem that im not understanding. i would really appreciate the help.
I have tried moving the variables into other scopes.
import java.io.*;
import java.net.*;
public class TestClient {
public static void main(String[] args) {
String hostName = "192.168.0.3";
int portNumber = 6666;
try ( //Connect to server on chosen port.
Socket connectedSocket = new Socket(hostName, portNumber);
//Create a printWriter so send data to server.
PrintWriter dataOut = new PrintWriter(connectedSocket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
) {
//Send data to server.
dataOut.println("a");
}catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
public static void sendToServer() {
//I want to control the print writer from this method.
//I have failed i all the ways i have tried.
}
}
You could move the Printer-Code (try try block) into the sendToServer-method and call it via
TestClient client = new TestClient();
client.sendToServer("this is a test");
Of course the sendToServer method needs to accept a parameter then. Even better would probably be to put the main method into a Starter class and decouple it from the Client-Class that you use for sending the data.

Sending and reading string to/from PrintWriter and BufferedReader

I have a server client that sends and receives info from the server socket using a PrintWriter for outgoing messages and a BufferedReader for incoming messages. I'm trying to test the client without a server connection in JUnit by sending strings to the client through the BufferedReader and reading the returned output from the PrintWriter.
class Client{
private BufferedReader incomingMessage;
private PrintWriter outgoingMessage;
private StringWriter output;
//Constructor for testing without server connection
public Client(){
output = new StringWriter();
outgoingMessage = new PrintWriter(output);
incomingMessage = new BufferedReader(new InputStreamReader(System.in));
}
//Methods for processing incoming messages and sending responses are
//omitted
//responses are sent using outgoingMessage.println("msg");
public void sendStringToInputStream(String msg){
incomingMessage = new BufferedReader(new StringReader(msg));
}
public String getOutputAsString(){
return output.toString();
}
}
This is the test I'm running.
public class ServerMessageTest {
private Client testClient;
private String output;
#Before
public void setUp(){
testClient = new Client();
}
#Test
public void testClientOutputMessage(){
testClient.sendStringToInputStream("GAME A OVER SEND OUTCOME");
output = testClient.getOutputAsString();
String testString = "GAME A OVER PLAYER 1 0 PLAYER 2 0";
Assert.assertEquals(testString, output.toString());
}
}
The test fails showing this:
org.junit.ComparisonFailure:
Expected :GAME A OVER PLAYER 1 0 PLAYER 2 0
Actual :
So there's an issue with reading the output message or setting the input message. I'm kinda new to IO stuff, so if someone could point out what I'm doing wrong I would greatly appreciate it!
Your variable names are bizarre. A BufferedReader isn't a message, and neither is a PrintWriter.
You aren't doing any output or input in this code. You need to call println() to send the message, and readLine() to receive it. Converting the reader and writer to strings accomplishes exactly nothing.
You need to create your reader and writer once per socket, not once per message.

Java, SSLSocket, receiving no answer

I'm trying to use SSL with IMAP in java. I do not want to use the IMAP class.
For some reason, when I send the n th message, I receive the answer to message n-2, and not to message n-1. Which means that I don't receive any answer to the first message sent until I send the second message. Can anyone spot what's wrong in the following minimal code ? (It is indeed minimal, apart from the println, which, I guess, help debugging)
import java.io.*;
import javax.net.ssl.*;
public class Mail{
static String server = "imap.gmail.com";
static String user = "straightouttascript#gmail.com";
static String pass = "azerty75";
public static void print (PrintWriter to, String text){
System.out.println("sent : "+text);
to.println(text+ "\r");
to.flush();
}
public static void read (BufferedReader from) throws InterruptedException, IOException {
do {
String line = from.readLine();
System.out.println("received: "+line);
} while (from.ready());
}
public static void main(String[] args){
try{
SSLSocket sslsocket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(server, 993);
System.out.println("Start connexion");
BufferedReader from = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
// read(from);
PrintWriter to = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sslsocket.getOutputStream())), true);
print(to,"a1 login "+user+" "+pass);
read(from);/*exepcted:
OK gimap ready
a1 OK login#host authenticated (Success)*/
sslsocket.close();
System.out.println("End connexion");
}
catch (Exception e){
e.printStackTrace();
}
}
}
IMAP is not a pingpong protocol. The server doesn't send one line in response to one of yours.
Rather, you send commands and the server sends information. The server is permitted to send you more information than you asked for, so you can get seven responses to one command, and you can even get a response without sending a command at all, which is then called an unsolicited response. Strange phrase. Unsolicited responses are used by some servers to notify you about new mail, by more to notify you about flag changes on messages, and by (almost?) all to notify you that they're about to close your connection.

Create listener for server socket response?

I have a server-client pair and I want to create a listener on the client end for new server responses. I am not sure how to do this, right now I can only interact in a direct synchronous way.
Here is the server:
public class TestServer {
public static void main(String[] args) throws Exception {
TestServer myServer = new TestServer();
myServer.run();
}
private void run() throws Exception {
ServerSocket mySS = new ServerSocket(4443);
while(true) {
Socket SS_accept = mySS.accept();
BufferedReader myBR = new BufferedReader(new InputStreamReader(SS_accept.getInputStream()));
String temp = myBR.readLine();
System.out.println(temp);
if (temp!=null) {
PrintStream serverPS = new PrintStream(SS_accept.getOutputStream());
serverPS.println("Response received: " + temp);
}
}
}
}
As you can see, it sends a response when it gets one. However in general I won't be sure when other servers I use send responses, so I would like to create an asynchronous listener (or at least poll the server for a response every half-second or so).
Here is what I'm trying on the client end:
protected static String getServerResponse() throws IOException {
String temp;
try {
BufferedReader clientBR = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
temp = clientBR.readLine();
} catch (Exception e) {
temp = e.toString();
}
return temp;
}
And just for reference, yes, sending over data from client to server works fine (it System.out's the data correctly). However, when I call the above function to try and retrieve the server response, it just hangs my application, which is an Android application in case that's relevant.
What I want from a function is just the ability to ask the server if it has data for me and get it, and if not, then don't crash my damn app.
On the client side create a ConnectionManager class which will handle all the socket I/O. The ConnectionManager's connect() method will create and start a new thread which will listen for server responses. As soon as it will receive a response it will notify all the ConnectionManager's registered listeners. So in order to receive asynchronously the server responses you will have to register a listener in ConnectionManager using its register(SomeListener) method.
Also, you can have a look at JBoss Netty which is an asynchronous event-driven network application framework. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.

Categories

Resources