i'm developing a simple test to connect via socket a java server application with an objective-c client side.
This is the java side:
BufferedReader dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = dis.readLine();
System.out.println("Message Received: " + message);
dis.close();
socket.close();
This is the objective-c side:
- (IBAction)click:(id)sender{
#try {
[socket sendString:[NSString stringWithFormat:#"%#",[toSend text]]];
}
#catch (NSException * e) {
NSLog(#"Unable to send data");
}
[toSend resignFirstResponder];
}
There are many problems:
in this way the output of server is:
Message Received: null
i read on this that readLine() needs to receive a string with "\n" that ends string, so i edit the line upon adding \n:
[socket sendString:[NSString stringWithFormat:#"%#\n",[toSend text]]];
but i retrieve an error on this line: EXC_BAD_ACCESS like in screenshot
What could be the problem? can someone help me?
I made a prototype in the Java side, and I don't have Objective-C/iOS or an Apple product.
I suspect the problem could be either in iOS or the Java side. So I hope you're sending text from iOS correctly.
Proposed code:
BufferedReader dis = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.defaultCharset()));
The class InputStreamReader need a defined character set. The charset is supposed to be required, not optional! according to Sun documentation.
For expediency, pls just include more code for clean compile next time. Thanks.
Good luck n try it out!
Tommy Kwee
According to your modified post/question, you got an exception from the Objective-C side. So possibly the problem is not on the Java side.
For debugging purposes, I am proposing to make the C code simpler, like...
BOOL success;
success = [socket SendString: #"Hello World!"];
if (success != YES) {
NSLog(#"Fail to send")
}
Notes:
I am not familiar with Objective-C, and I could not find documentation from Apple on it. If you know, please tell me/us.
I did not use code "[toSend text]" because I did not find any references on it.
Other iOS developers are saying "%#" is meant for objects. Is code [toSend text] an object?
Finally, making a socket connection and achieving communication should be possible and not so hard, and eventually we'll get there. So let's do it!
Related
These questions may sound silly, but I am new to this networking thing.
I have been trying for quite a few days now to implement a client that works with a Twisted server, but I am failing to get any response back from the server. I have read a lot of docs and watched a few tutorials and I got some of the stuff fixed and got some of the concepts better understood.
Before I step on to asking any questions, I wanna show you my code first. This is what I use to talk to the Twisted-based server:
val socketfactory: SocketFactory = SocketFactory.getDefault()
val socket = socketfactory.createSocket(host, port)
socket.keepAlive = true
socket.tcpNoDelay = true
val isSocketConnected = socket.isConnected //this checks socket's connectivity
val dOut = DataOutputStream(socket.getOutputStream())
val dIn = DataInputStream(socket.getInputStream())
val teststring = "Hi server!"
dOut.writeUTF(teststring)
Log.d("MILESTONE", "MESSAGE SENT AT THIS POINT, Socket is connected ?: $isSocketConnected")
var testreader = ""
while (true) {
testreader = dIn.readUTF()
Log.d("READING:", "RECEIVED THIS: $testreader")
}
My code seems to never get to the second "Log" line. It never gets there. I assume that's because I never get any input from the server. This is getting me confused. Because "socket.isConnected" returns true. Doesn't that mean there is an ongoing connection between the client (me) and the server ? But when I send any output the server doesn't talk back.
So my questions are:
1- Am I doing something wrong? Why do I receive no talk from the server and it blocks the code?
2- Is SocketFactory necessary ?
3- Is there any library that communicates with Twisted from Java ?
Thanks in advance !
For everyone who's struggling to communicate with a Twisted-running python server, I came with the absolutely best solution ever! After inspecting Twisted's open source code, I came to realize it has a "LineReceiver" class that only responds to a message if the line is finished. In other words, you can keep sending data forever and it will never respond until you finish the line and start a new one. Twisted will know the line has finished when a delimiter is used. (It is configured on the server-side). Most servers running Twisted will use a line delimiter : "\r\n"
That's the tricky thing! Once you send that little string, it will start responding to you. Here it is in an example :
val dOut = DataOutputStream(socket.getOutputStream()) //This is my favorite way of sending data!
val dIn = socket.getInputStream().bufferedReader(Charsets.UTF_8) //This is my favorite way of reading data !
val teststring = "hi server! \r\n" //This is the tricky part !
That's it ! all you have to do after that is read the lines from the bufferedReader, like this !
var testreader: List<String>
while (true) {
testreader = dIn.bufferedReader(Charsets.UTF_8).readLines()
for (line in testreader)
Log.e("MILESTONE", line)
}
After I started reading the input, I found out the server started actually sending me strings and communicating back with me. I hope everyone will get their codes working concerning this or any other thing!
I'm trying to write a socket program in Java that intercepts data/packets.
I've successfully written this in python:
import socket
def createListener(port):
srvSock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
srvSock.bind(('localhost', port))
srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
while True:
raw_data, addr = srvSock.recvfrom(65536)
print('data: ' , raw_data)
createListener(80)
This is my basic Java socket program
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(80);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String)dis.readUTF();
System.out.println("data: "+str);
ss.close();
} catch(IOException i){
System.out.println(i);
}
}
However, when run, it doesn't intercept all data moving through the port on the network like the python program does. Specifically this line srvSock.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) in the Python script enables the socket to listen to the port and capture/intercept the entirety of the data going through it.
I cannot find a Java alternative to this syntax or any solution to my problem at all for that matter.
I would appreciate any help in how to use sockets to intercept packets on a network port.
Thanks
Im fairly certain what you are trying to do cannot be done with Java. It looks like you are trying to use "promiscuous mode", but Java sockets cannot be started in promiscuous mode. Java sockets are an end-to-end implementation: they can't listen on the network port for all traffic. For sure the network port would have to be in promiscuous mode, but I don't think Java is the right choice for you.
The only thing I can think of that might get you there would be doing a native call in something like JNI, but I wouldn't even really know where to start with that.
Here is a really old post that I found that is kind of related: java socket and web programing
From the looks of it, you're trying to read incoming bytearrays as string lines.
If that is so, this is what I do to read lines without missing a single line (In Kotlin):
socket.getInputStream().bufferedReader(Charsets.UTF_8).forEachLine {
it -> { /* Do what you wanna do with the input */ }
}
In Java, it's much less abstract :
BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.UTF_8), 8 * 1024)
Then, use lines from this buffered reader as a line sequence to read your incoming lines.
I have a problem with a Java-Python Socket. My objective is to send a Json object from java application to python script via socket tcp and receive a response but the socket is blocked after Json sending. In the following there is my code:
try {
Socket socket = new Socket(dstAddress, dstPort);
is = new DataInputStream(socket.getInputStream());
os = new DataOutputStream(socket.getOutputStream());
PrintWriter pw = new PrintWriter(os, true);
pw.println(jsonObject.toString());
System.out.println("Send to the socket jsonObject.");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String response = in.readLine();
System.out.println("Response: " + response);
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
In the following lines the python code:
HOST = "192.168.1.101" #localhost
PORT = 7011
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while (1):
print("\n\nAttending for client.....\n\n")
conn, addr = s.accept()
print("Connected by: " , addr)
data = ""
while 1:
temp = conn.recv(1024).decode()
if not temp:
break
data = data + temp
print("JSON Received!!!!!")
imageJson = {}
imageJson = json.loads(data)
# responding to the client
response = DbImages[elem[0]]
resp = "Prova"
conn.send(resp.encode())
If I terminate the java code (ctrl+C) the socket exit from block and json arrive to python. What is the problem? The problem seems to be in.readLine (). If I delete that statement then the socket works without blocks.
Your Python code is waiting for the Java side to finish and send EOF before responding (that’s what it means to recv until you get an empty bytes).
Your Java code is waiting for the Python side to respond before closing the socket.
So, they’re both waiting for each other.
Removing the readLine means the Java code is no longer waiting for anything, so it just hangs up on the Python code as soon as it’s done sending, which does make the problem go away—but it isn’t much of a solution if you actually needed a response.
So, what should they be doing? Well, there are a few different options.
Use a framed protocol, where the Java side either sends a “message-done” delimiter after each message or sends a header (with, e.g., the byte length of the message) before each one. So, the Python code can read until it has a complete message, instead of until EOF.
If you encode your JSON in compact format with everything but printable ASCII escaped, then the delimiter can just be a newline (at which point you’re using JSONlines as your protocol), and the Python code can use makefile on the socket and call readline instead of looping over recv.
Cheat and use JSON as if it were a framed protocol. It isn’t, but as long as the only top-level values you ever send are objects and arrays, it works. Then the Python code can use raw_decode (see the json module docs) after each receive until it succeeds.
If you’re only ever going to send a single message, you can just half-shutdown the socket (close the write end) from Java, and then Python will get its EOF and respond on the still-open other side of the socket. (This may sound hacky, but it’s actually perfectly common—it’s how web browsers traditionally work, although HTTP 1.1 made things a bit more complicated.)
Your response is not a line, as it doesn't seem to contain a line ending.
That means it readLine will read forever.
Try adding a newline to your response to make readLine happy:
resp = "Prova\n"
I want to Implement a Chat Server and Client using java like gTalk, what Type of connection should i work on(XAMPP or Socket Connection), please guide me, i want to implement this for my final sem project.It will be a kind of Instant Messaging like gTalk and skype.
Please give me some idea/outlines or links where i can read some stuff, so that i can study some and start implementing those.
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
Thanks
XAMPP is mostly a package of software mainly Apache webserver, MySQL, Perl and PHP.
Since you want to code in java, the chat server would be based on Socket Programming.
As per the code snippet you have posted is a simple java server, which can listen to request and print on screen what's been send to it.
On doing some google search found a simple step by step explanation of creating chat application in java.
http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html
I have followed Google's tutorial on using Bluetooth on Android. On the server side which is a computer I've used Bluecove.
I have managed to establish the connection between the devices (they weren't paired first and when I ran the program both of the devices asked to accept the pairing). Now I have problem with sending data over the connection. In the Managing a connection section there's a method called write. With that function I'm not able to send anything. It doesn't fail (no errors) but the message doesn't reach the server.
On the server after accepting the connection (acceptAndConnect()) I have the following
DataInputStream inputStream = new DataInputStream(connection.openInputStream());
while(true) {
String input = "";
char c;
while(((c = inputStream.readChar()) > 0) && (c != '\n')) {
input += c;
}
System.out.println("Received: " + input);
}
It doesn't print anything.
The code on the client side is exactly the same as in the tutorial. Where is the problem?
Edit. Actually, if I print c in the server code shown up I get something strange. I've tried to send hello\n. When printing c, I get 桥
汬
漊
After a little bit of more googleing I found Jon Skeet's answer and changed the DataInputStream to an InputStream wrapped in an InputStreamReader and that one in a BufferedReader. Then I can use readLine() and I get the characters right.