using a socket to print raw text - java

i'm working on an application to print to my local wifi printers but over 9100 port i have no response, over the 515 i have "connection refused".
the code is this:
Socket test = new Socket();
Socket sock = new Socket(ip,9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.printf("========================\n");
oStream.printf("PROVA STAMPA\n");
oStream.printf("HELLO SLOW SUD\n");
oStream.printf("========================"+"\n");
oStream.close();
sock.close();
and also i have try this:
Socket objSocket = new Socket();
InetSocketAddress objEndPoint = new InetSocketAddress(ip, 9100);
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 6000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("========================\nPROVA STAMPA\nHELLO SLOW SUD\n========================\n").getBytes());
objOutputStream.close();
objSocket.close();

Related

Trouble with sending / receiving data

So I am attempting to send data to myself and receive the data then print it, now I have been testing for a while and I have noticed its not sending anything, in fact, maybe it is and I am not receiving it properly, I need assistance with this please.
This is what I am using to send data
String host = "127.0.0.1";
int port = Options.port;
Socket socket = new Socket(host, port);
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(msg + "\n");
bw.flush();
This is what I am using to receive, I always use this method and it never works so I would not be surprised if this was the root cause.
ServerSocket serverSocket = new ServerSocket(Options.port);
System.out.println("[Listening on port] " + Options.port);
while(true){
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println(message);
}
1) replace:
Socket socket = serverSocket.accept();
socket = serverSocket.accept();
with:
Socket socket = serverSocket.accept();
2) If this does not solve the problem, set ports as int values, without Options.port

UDP to TCP Java

I am trying to figure out how to make this piece of code into TCP instead of UDP
DatagramPacket answerDP = null;
answerDP = new DatagramPacket(new byte[110], 110);
What do I use in TCP instead of DatagramPacket?
The same goes for DatagramSocket, what do I use in TCP instead?
DatagramSocket socket = null;
socket = new DatagramSocket();
socket.send(packet);
socket.setSoTimeout(5000); //wait for answar max. 1 sec.
socket.receive(answerDP);
DatagramSocket in TCP is basically ServerSocket
So for example, to initialise it:
ServerSocket welcomeSocket = new ServerSocket(6789);
And the client socket would be something like:
Socket clientSocket = new Socket("localhost", 6789);
Set timeout works the same
socket.setSoTimeout(5000);
See a simple example here

Java Socket Client not Recieving Data

I have created my first test application implementing a socket server. I am having some issues getting the client to receive data, but the server gets data just fine. Here is the server:
ServerSocket socket = new ServerSocket(11111);
System.out.println("CREATING SERVER...");
while (true) {
Socket SERVER_WORK = socket.accept();
BufferedReader clientIN = new BufferedReader(new InputStreamReader(SERVER_WORK.getInputStream()));
PrintWriter outSend = new PrintWriter(SERVER_WORK.getOutputStream());
String ClientSTR = clientIN.readLine();
System.out.println("Client 1: " + ClientSTR);
String toClient = "Hello";
outSend.write(toClient + '\n');
}
And here is the client:
System.out.println("CONNECTING TO SERVER...");
while (true) {
Socket clientSocket = new Socket(server, 11111);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream toServere = new DataOutputStream(clientSocket.getOutputStream());
Scanner in = new Scanner(System.in);
toServere.write(in.nextLine().getBytes());
if (fromServer.ready())
System.out.println(fromServer.readLine());
clientSocket.close();
}
Everything works properly except for the client receiving data.
I found the solution: I needed a '\n' at the end of the line for the DataOutputStream/PrintWriter for the BufferedReader to work properly.

Debug socket communication in eclipse

I need to know the flow of the below given program:
Client Class:
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
Server Class:
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();
When I run the server class and then the client class, how does the flow of the program work?
It flows exactly how it reads.
Server binds the socket to port 1101, listens for connections
1. ServerSocket sersock = new ServerSocket(1101);
2. sock = sersock.accept();
Client connects to server and sends "hello" and closes connection.
1. Socket sock = new Socket("localhost", 1101);
2. PrintWriter write = new PrintWriter(sock.getOutputStream());
3. write.println("Hello");
4. write.close();
Server reads and prints line from input stream after a connection has been made then closes the reader.
3. InputStreamReader read = new InputStreamReader(sock.getInputStream());
4. BufferedReader buf = new BufferedReader(read);
5. System.out.println(buf.readLine());
6. buf.close();

Cannot open output stream

For some reasons i have to set up on android SocketServer which waits for connection with computer. All is going good, socket with client (computer) is creating, but streams don't open. It's just pausing without any error or message.
client:
s = new Socket(ip, 4567);
ois = new ObjectInputStream(s.getInputStream());
System.out.println("ois..");// not showing, so can't open input stream
oos = new ObjectOutputStream(s.getOutputStream());
System.out.println("oos.."); // same here
server:
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
ois = new ObjectInputStream(client.getInputStream());
System.out.println("ois.."); // not showing
oos = new ObjectOutputStream(client.getOutputStream());
System.out.println("oos.."); // not showing too
System.out.println("Stream,s opened");
My apk has INTERNET premissions. I'm using 4567 port. Any other application doesn't block the port.
What can be wrong?
Try opening the ObjectOutputStream first in your server.
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
System.out.println("Stream,s opened");
I don't see any timeouts in there, which is why it just stops.
There's probably some network problem; have you verified the IP address of the device is correct?
It's unlikely, but there could be some firewall rule blocking the connection.

Categories