I am a beginner in developing the java applications. I'm making a chat application on android. I use a thread to serve the client who comes in, but when the client has connected to the server I can not retrieve the data contained in the socket, but when a client connection is lost, data can be displayed. I use the ReadLine method to read data from the socket.
This is the program code on the server side:
package server;
import java.net.*;
import java.io.*;
import java.util.Vector;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class Server {
public static void main(String[] args)throws IOException, InstantiationException,
IllegalAccessException {
ServerSocket servsocket = null;
Socket sock = null;
byte[] bytebuffer = new byte[512];
try {
System.out.println("SERVER IS RUNNING...");
servsocket = new ServerSocket(28000);
while(true){
sock = servsocket.accept();
System.out.println(servsocket.isBound());
System.out.println("Port "+servsocket+" Ready!!!");
System.out.println("Accept connection requests from " + sock);
System.out.println("From CLIENT "+sock.getInetAddress()+ " and PORT " +
sock.getPort());
ChatThread thread = new ChatThread(sock);
System.out.println("Thread is running");
thread.run();
}
} catch (IOException ioe) {
System.out.println(ioe);
}
finally{
try {
servsocket.close();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
}
class ChatThread extends Thread{
static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
private Socket sock;
private BufferedReader in ;
private PrintWriter out;
public ChatThread (Socket socket) throws IOException {
this.sock = socket;
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()));
byte[] bytebuffer = new byte[512];
int receivemssg;
}
public void run(){
int recvMsgSize;
byte[] bytebuffer = new byte[512];
String readsocket;
try {
readsocket = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Below the display on the server side when the program starts. I tried to send the word "Hello ...." from the client side. Can be seen that the thread is not running.
Server is running...
true
Port ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=28000] Ready!!!
Accept connection requests fromSocket[addr=/172.17.231.254,port=3567,localport=28000]
From CLIENT /172.17.231.254 and PORT 3567
Thread is Running...
When I replace the readline method on a thread with getInputStream the thread can be run from the client and the message can be displayed. This is the code that I enter the thread to replace the readline method that I used before.
public ChatThread (Socket socket) throws IOException {
this.sock = socket;
in = sock.getInputStream();
out = sock.getOutputStream();
byte[] bytebuffer = new byte[512];
int receivemssg;
}
public void run(){
int recvMsgSize;
byte[] bytebuffer = new byte[512];
System.out.println("Thread is Running...");
String masuk = new String(bytebuffer);
System.out.println(bytebuffer);
System.out.println(in.toString());
System.out.println("thread successfully executed !!!");
synchronized (chatthread) {
chatthread.addElement(this);
}
try {
while ((recvMsgSize = in.read(bytebuffer)) != -1) {
out.write(bytebuffer, 0, recvMsgSize);
System.out.println("The length of a character is received and returned "+bytebuffer.length);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
but the next problem is I can not bring up the contents of a socket in a string / text that appears is as follows:
Port ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=28000] Siap!!!
Accept connection requests fromSocket[addr=/172.17.231.254,port=3577,localport=28000]
From CLIENT /172.17.231.254 and PORT 3577
Thread is Running...
[B#7c6768
java.net.SocketInputStream#1690726
thread successfully executed !!!
The length of a character is received and returned 512
Please Help me, thanks :) GBU guys...
See the developer docmentation
public final String readLine ()
Since: API Level 1
Returns a string containing the next line of text available from this stream.
A line is made of zero or more characters followed by '\n', '\r', "\r\n"
or the end of the stream. The string does not include the newline sequence.
readLine() will block and not return until it either sees an end-of-line condition such as a newline character, or the end of the stream is reached, which is probably what happens when the connection is lost.
If you want to use readLine() you need to send "Hello....\n" or otherwise append a terminating character for readLine() to see.
Related
I'm playing around ServerSocket on Android as the server part. I don't understand how it behaves. Here are what I tested :
A1. Instantiates a ServerSocket on Android
A2. ServerSocket sends "hello" to client
A3. Client can read the "hello" and can answer back to ServerSocket
A4. ServerSocket on Android receives the answer from the client
=> That works perfectly
Now I want the client to be the first to send a message to ServerSocket :
B1. Instantiates a ServerSocket on Android
B2. Client sends data to ServerSocket
B3. ServerSocket receives the data from client
B4. IMPOSSIBLE TO REPLY to the client
May that be a possible normal behaviour ?
Thanks
here is the source code
public void startServer()
{
log("startServer");
UUID uuid = UUID.randomUUID();
final String sessionId = uuid.toString().replace("-", "");
log("Session ID = " + sessionId);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
while (stopServer == false) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(7777);
final Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
String strFromClient = "";
int i = 0;
while (i != -1) {
try {
i = inputStream.read();
if (i != -1)
strFromClient += (char) i;
}catch (Exception e){
break;
}
}
inputStream.close();
OutputStream outputStream = socket.getOutputStream();
String strToClient = "test";
byte[] cArray = strToClient.getBytes();
outputStream.write(cArray);
outputStream.flush();
outputStream.close();
socket.close();
serverSocket.close();
log("end server");
} catch (Exception e) {
//log(e.toString());
}
}
}
});
t.start();
}
Ok I found the solution ! The error was because the C# client was not sending the "-1" value (this is only triggered after closing a stream or stuff like that).
The solution is on the Android side, and the reading of the data from the client is now done as follow :
InputStream inputStream = socket.getInputStream();
String strFromClient = "";
int available = inputStream.available();
Log.d("intelsms", "available from client:" + available);
for (int i=0;i<available;i++){
int c = inputStream.read();
strFromClient+=(char)c;
}
I use the "available()" method in order to know how many bytes are available for reading from the client.
OUF !
I'm trying to create a proxy in Android and I have to use sockets . I've read many tutorials and came up with following code. Unfortunately browser doesn't seem to get any data and after some time it displays standard web page saying that web page is not available. What might be the cause? Thanks for your help.
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(9902, 0, InetAddress.getByName("localhost"));
} catch (Exception e) {
e.printStackTrace();
}
if (serverSocket != null) {
while (!Thread.currentThread().isInterrupted()) {
try {
Socket socket = serverSocket.accept();
new Thread(new RunnableToReadSocketData(socket)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class RunnableToReadSocketData implements Runnable {
private final Socket clientSocket;
public RunnableToReadSocketData(Socket socket) {
this.clientSocket = socket;
}
#Override
public void run() {
Socket serverSocket = null;
try {
InputStream streamFromClient = clientSocket.getInputStream();
PrintWriter streamToClient = new PrintWriter(clientSocket.getOutputStream());
StringWriter writer = new StringWriter();
IOUtils.copy(streamFromClient, writer);
String requestString = writer.toString();
int firstSpace = requestString.indexOf(" ");
int secondSpace = requestString.indexOf(" ", ++firstSpace);
String url = requestString.substring(firstSpace, secondSpace);
Uri uri = Uri.parse(url);
String urlWithoutProtocol = uri.getHost();
System.out.println("==============Reading Socket==============\n" + clientSocket.toString() + "\n" + requestString);
serverSocket = new Socket(urlWithoutProtocol, 80);
PrintWriter streamToServer = new PrintWriter(serverSocket.getOutputStream(), true);
streamToServer.write(requestString);
streamToServer.flush();
InputStream streamFromServer = serverSocket.getInputStream();
StringWriter writerResponse = new StringWriter();
IOUtils.copy(streamFromServer, writerResponse);
String responseString = writerResponse.toString();
System.out.println("==============RECEIVED==============\n" + serverSocket.toString() + "\n" + responseString);
streamToClient.write(responseString);
streamToClient.flush();
streamToClient.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
if (clientSocket != null) {
clientSocket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
You're doing this wrong. After you process the CONNECT command you need to start two threads to copy bytes over the connection, one in each direction. Don't attempt to read the entire request before you send anything; ditto the response. Just copy bytes as you receive them.
When you read EOS on one socket, shutdown the other socket for output and exit that thread. If you've already shutdown the socket you read the EOS from, close both and exit the thread. You need this in case either end does a shutdown, to propagate it properly.
I agree with previous.
The general principle is:
Client connects
Start reading thread
Receive request
Parse destination
Open socket to destination
Forward request
For every Read on the destination socket, do a write on the client socket
For every Read on client socket, do a write on destination socket
If either socket closes (errors), close the other
So, two InputStream's, two OutputStreams, and just ferry data across them.
Hey community I have the following ServerSocket which should listen to port 53000 and log any received data. However, I cannot seem to get past the server.accept() blocking call.
public void run() {
SocketServer server = new ServerSocket(53000);
//---buffer store for the stream---
byte[] buffer = new byte[1024];
//---bytes returned from read()---
int bytes;
//---keep listening to the InputStream until an
// exception occurs---
while (true) {
try {
socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = in.readLine();
Log.i("received response from server", str);
in.close();
socket.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (Exception e){
server.close();
Log.e(TAG, e.getMessage());
}
}
}
I have also given the application the INTERNET permission in the Manifest file.
()
To add to the mystery, I have also verified client responses get sent to that port.
Is there something in particular I need to do to make this work?
Thanks.
Your code is very messy and won't even compile. I made some adjustments so that i could test your code, and it's working fine. Here is the test application I used:
package com.test.stackoverflow
import java.io.BufferedReader;
public class ServerSocketTestActivity extends Activity {
/** Called when the activity is first created. */
private static String TAG = "ServerSocketTest";
private ServerSocket server;
Runnable conn = new Runnable() {
public void run() {
try {
server = new ServerSocket(53000);
while (true) {
Socket socket = server.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String str = in.readLine();
Log.i("received response from server", str);
in.close();
socket.close();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(conn).start();
}
#Override
protected void onPause() {
super.onPause();
if (server != null) {
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Using this code and netcat running via adb shell I was able to connect and communicate with the application.
When working with The Client Declare these methods
To access Streams
// gets the input stream // ObjectInputStream input;
// gets the output stream // ObjectOutputStream output;
// ServerSocket server;
// Socket connection;
maybe you have a another class to access the socket;
server = new ServerSocket(5001, 100);
// step 1 create socket connection
server = new ServerSocket(5001, 100);
while(the condition is true)
// step 2 wait for connection
// step 3 get streams
// step 4 : process the connection
// step 5 : close connection
In my project i have to read barcodes using barcode scanner Symbol CS3070 through bluetooth. i.e; i have to establish a connection between android device and barcode scanner through bluetooth. Can any one tell me how to read values from barcode reader and how to setup for communication? I've already read the Bluetooth Developer Guide, and I don't want to use Barcode Reader in Bluetooth Keyboard Emulation (HID) mode (I've some textview that can be filled using soft keyboard and Barcode Reader and I can't control the focus)
I'd use a thread like this to communicate with a reader
private class BarcodeReaderThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public BarcodeReaderThread(UUID UUID_BLUETOOTH) {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BarcodeScannerForSGST", UUID_BLUETOOTH);
/*
* The UUID is also included in the SDP entry and will be the basis for the connection
* agreement with the client device. That is, when the client attempts to connect with this device,
* it will carry a UUID that uniquely identifies the service with which it wants to connect.
* These UUIDs must match in order for the connection to be accepted (in the next step)
*/
} catch (IOException e) { }
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
try {
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
InputStream mmInStream = null;
// Get the input and output streams, using temp objects because
// member streams are final
mmInStream = socket.getInputStream();
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
// Read from the InputStream
bytes = mmInStream.read(buffer);
if (bytes > 0) {
// Send the obtained bytes to the UI activity
String readMessage = new String(buffer, 0, bytes);
//doMainUIOp(BARCODE_READ, readMessage);
if (readMessage.length() > 0 && !etMlfb.isEnabled()) //Se sono nella parte di picking
new ServerWorker().execute(new Object[] {LEGGI_SPED, readMessage});
}
socket.close();
}
}
catch (Exception ex) { }
} catch (IOException e) {
break;
}
}
}
/**
* Will cancel the listening socket, and cause the thread to finish
*/
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
Thanks
I just received my device and when I paired and connected the device it automatically sends the data to the currently focused EditText. What version of Android are you using because I tried it on ICS and JB and it worked this way.
I have not tested it in any earlier versions.
Edit:
I downgraded my phone to Gingerbread and found out it does not work the same way but I have a solution:
This is important! >> First you must scan the barcode in the manual that says "Serial Port Profile (SPP)".
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter.isEnabled())
{
new BluetoothConnect().execute("");
}
public class BluetoothConnect extends AsyncTask<String, String, Void>
{
public static String MY_UUID = "00001101-0000-1000-8000-00805F9B34FB";
#Override
protected Void doInBackground(String... params)
{
String address = DB.GetOption("bluetoothAddress");
BluetoothDevice device = btAdapter.getRemoteDevice(address);
try
{
socket = device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
btAdapter.cancelDiscovery();
socket.connect();
InputStream stream = socket.getInputStream();
int read = 0;
byte[] buffer = new byte[128];
do
{
try
{
read = stream.read(buffer);
String data = new String(buffer, 0, read);
publishProgress(data);
}
catch(Exception ex)
{
read = -1;
}
}
while (read > 0);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(String... values)
{
if (values[0].equals("\r"))
{
addToList(input.getText().toString());
pickupInput.setText("");
}
else input.setText(values[0]);
super.onProgressUpdate(values);
}
}
This is an incomplete version of my working code but you should get the gist.
I hope this solution works for you as well!
On Android I tried to implement a simple TCP Listener Thread (or copied it from anywhere). It should simply wait for a Text and then do something. The Text is sent, this part works, but this listener-Thread doesn´t even create the Socket for listening correctly.
Has anyone an Idea, whats wrong or another simple approach for me?
The text is defined b myself and not html. I only found much too complicated http-handlers.
import java.lang.*;
import java.io.*;
import java.net.*;
public class Client implements Runnable {
public static void main(String args[]) {
System.out.print("Listening Thread started\n");
try {
Socket skt = new Socket("localhost", 2999);
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");
System.err.println(e);
}
}
public Client () {
}
#Override
public void run() {
// TODO Auto-generated method stub
main(null);
}
}
The code you showed is used to create a client socket, not a server socket. see below an example of TCP server socket, taken from SystemBash:
class TCPServer
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}