So basically I want to create some kind of controller for my pc, where on pc, runs java socket server. The server is working, I have tried it. Also, Port Forwarding in the router is allright. Source code is here :
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_BOLD = "\033[1m";
public static final String ANSI_RED = "\u001B[31m";
public static void main(String[] args) throws IOException {
// Creating server instance
ServerSocket serverSocket = new ServerSocket(2000);
Socket socket = serverSocket.accept();
System.out.println(ANSI_GREEN + "Controller connected" + ANSI_RESET);
// Input Output streams
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
String command = "";
do {
command = dataInputStream.readUTF();
switch (command.trim()){
case "volume up":
Runtime.getRuntime().exec("cmd /c start src\\sk\\dzurik\\main\\volup.bat");
break;
case "volume down":
Runtime.getRuntime().exec("cmd /c start src\\sk\\dzurik\\main\\voldown.bat");
break;
default:
// Unknown command
break;
}
}while (!command.equals("stop"));
socket.close();
System.out.println(ANSI_RED + "Controller disconnected" + ANSI_RESET);
}
}
And it is supposed to interact with the android app, but I got an error and I can't quite figure out why is that so, here is the source code for ActivityMain :
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private boolean connected;
private DataOutputStream dataOutputStream;
private Socket socket;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Pripájanie na server
try {
socket = new Socket("172.0.0.1",2000);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Not Connected!",
Toast.LENGTH_LONG).show();
}
Button VolumeUp = (Button) findViewById(R.id.VolumeUpButton);
VolumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
send("volume up");
} catch (IOException e) {
e.printStackTrace();
}
}
});
Button VolumeDown = (Button) findViewById(R.id.VolumeDownButton);
VolumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
send("volume down");
} catch (IOException e) {
e.printStackTrace();
}
}
});
Button DisconnectButton = (Button) findViewById(R.id.DisconnectButton);
DisconnectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
send("stop");
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public void send(String command) throws IOException {
if (socket != null){
dataOutputStream.writeUTF(command);
}
else Toast.makeText(MainActivity.this, "Socket is not defined",
Toast.LENGTH_SHORT).show();
}
public void disconnect() throws IOException {
if (socket != null){
socket.close();
}
else Toast.makeText(MainActivity.this, "Socket is not defined",
Toast.LENGTH_SHORT).show();
}
}
So if you could help me with it, I would be thankful, <3
Well the reason for that was silly. When I'm loading data from Stream it says nextLine which means the ending point is \n so I could never get that line because I haven't put it there.
Related
Currently, I have a server(python) and client(android). I am trying to send data from an android to server to another Android using socket. The problem is data looks fine in the server, but garbage value is included when an Android get the Data from server. If I varied the size of string, then it sometimes did not receive the data and after additional sendings, the weird garbage value was received. I think it was some kind of buffer related problem, but I could not figure out the exact problem.
This is output from server : outputFromServer
This is output from android : outputFromAndroid
This is server.py. I am getting data from a client and send it to other clients.
import socket
import time
import select
host = 'myIP' # Symbolic name meaning all available interfaces
port = myPortNumber # Arbitrary non-privileged port
server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#socket.SOCK_STREAM
server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)#assure reconnect
server_sock.bind((host, port))
server_sock.listen(5)
print(server_sock)
sockets_list = [server_sock]#socket list... multiple clients
clients = {}#socket is the key, user Data will be value
print("Waiting")
def receive_message(client_socket):
try:
data = client_socket.recv(1024)
print("receive Message : " +data.decode('utf-8'))
#return data.decode('utf-8')
return {"data" : data}
except:
return False
while True:
read_sockets, _, exception_sockets = select.select(sockets_list, [], sockets_list)#read, write ,air on
for notified_socket in read_sockets:
if notified_socket == server_sock: #someone just connected
client_socket, client_address = server_sock.accept()
print(client_socket)
print(client_address)
user = receive_message(client_socket)
if user is False:
print("USER FALSE")
continue
sockets_list.append(client_socket)
clients[client_socket] = user
print("ACCEPTED connection")
else:
message = receive_message(notified_socket)
print("IN ELSE : " + message['data'].decode('utf-8'))
if message is False :
sockets_list.remove(notified_socket)
del clients[notified_socket]
continue
user = clients[notified_socket]
#share this message with everyBody
for client_socket in clients:
print("for loop")
if client_socket != notified_socket:
message_to_send = message['data']
client_socket.send(len(message_to_send).to_bytes(2, byteorder='big'))
client_socket.send(message_to_send)
for notified_socket in exception_sockets:
sockets_list.remove(notified_socket)
del clients[notified_socket]
Here is my Android(java) code for client
package com.example.testing;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ConnectTcp {
private String TAG = "tcp";
private DataOutputStream dos;
private DataInputStream dis;
private Socket socket;
private Thread socketThread;
public ConnectTcp() {
}//connectTcp
void sendMessage(final String message){
socketThread = new Thread() {
public void run(){
try {
if (Thread.interrupted()) { throw new InterruptedException();}
while(!Thread.currentThread().isInterrupted()) {
// ...
try {
socket = new Socket(remoteIP, port);
Log.e(TAG, "success");
}catch (IOException ioe) {
Log.e(TAG,"ERROR");
try {
dos = new DataOutputStream(socket.getOutputStream()); // output
dis = new DataInputStream(socket.getInputStream()); // inpu
dos.writeUTF("CONNECT TO SERVER : "+ message);
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
try {
while (true) {
line = (String) dis.readUTF();
Log.w("------서버에서 받아온 값", "" + line);
}
} catch (Exception e) {
}
}
}
} catch (InterruptedException consumed){
/* Allow thread to exit */
}
}//run
};//Thread
socketThread.start();
}
void sendAdditionalMessage(final String data) throws IOException {
Thread sendMessageThread = new Thread() {
#Override
public void run() {
super.run();
try {
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(data);
} catch (IOException e) {
e.printStackTrace();
}
}
};
sendMessageThread.start();
}
void closeSocket() throws IOException {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
socketThread.interrupt();
}
}//class
This is MainActivity.java
package com.example.testing;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import okhttp3.FormBody;
import okhttp3.RequestBody;
public class MainActivity extends AppCompatActivity {
String getData;
TextView textView;
Button bt_connect;
Button bt_disConnect;
TextView tv_hanium;
int data = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_connect = findViewById(R.id.bt_connect);
bt_disConnect = findViewById(R.id.bt_cancel);
Button bt_goToLogin = findViewById(R.id.bt_goToLogin);
final Button bt_send = findViewById(R.id.bt_send);
final ConnectTcp connectTcp = new ConnectTcp();
bt_connect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
connectTcp.sendMessage("hello world");//connect to server
}
});
bt_disConnect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
connectTcp.closeSocket();//close socket
} catch (IOException e) {
Log.d("tcp", "fail to close ");
e.printStackTrace();
}
}
});//데이터 전송 중단
bt_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
connectTcp.sendAdditionalMessage("150");// send data
} catch (IOException e) {
e.printStackTrace();
}
}
});
bt_goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent goToLogin = new Intent(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(goToLogin);
}
});
}
}
The Java DataOutput.writeUTF method sends the length of the string it writes. If you don't want to modify the Java side, you have to modify your receive_message Python function to read the length field. Here's a start:
def receive_message(client_socket):
try:
data = client_socket.recv(2)
# TODO: ensure 2 bytes were returned
expected_length = int.from_bytes(data, byteorder="big")
data = client_socket.recv(expected_length)
if len(data) < expected_length:
# TODO: add a loop to read more data from the socket
pass
# TODO: write your own "modified utf-8" decoder - see
# https://docs.oracle.com/javase/8/docs/api/java/io/DataInput.html
print("receive Message : " + data.decode('utf-8'))
Java's DataOutput and DataInput are not really designed for general input/output, and they make integrating with other programming languages difficult as you've discovered. If I were you I would consider migrating to a different protocol, for example one that is based on reading and writing lines of text.
So,It's like the title Say's i have created a Simple android Socket server and Client which Connects over Wifi. Both devices are on the same Wifi Network. The Server Shows the IP Address on which it host the connection....And in the client You have to write the IP Address on which Server is hosted.....
So, My problem is when i am Entering The IP Address in the Client The Server shows "Connected". But when i start to send messages it doesn't send the message to other side neither Server to Client nor Client to server.
//#Server Java File
package inc.teckzy.wif_chat;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Helper extends AppCompatActivity {
EditText smessage;
TextView chat,display_status;
String str,msg="";
int serverport = 6666;
ServerSocket serverSocket;
Socket client;
Handler handler = new Handler();
WifiManager wmanager;
Boolean Alive;
DataOutputStream out;
DataInputStream in;
Button button_sent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helper);
wmanager = (WifiManager) getSystemService(WIFI_SERVICE);
#SuppressWarnings("deprecation")
String ip = Formatter.formatIpAddress(wmanager.getConnectionInfo().getIpAddress());
smessage = (EditText) findViewById(R.id.smessage);
chat = (TextView) findViewById(R.id.chat);
display_status = (TextView) findViewById(R.id.display_status);
Thread serverThread = new Thread(new serverThread());
serverThread.start();
Alive = serverThread.isAlive();
display_status.setText("Hosted on: " + ip);
button_sent = (Button) findViewById(R.id.button_sent);
button_sent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thread sentThread = new Thread(new sentMessage());
sentThread.start();
}
});
}
class sentMessage implements Runnable{
#Override
public void run() {
try{
client = serverSocket.accept();
out = new DataOutputStream(client.getOutputStream());
str = smessage.getText().toString();
msg = msg+"\n Server:" +str;
handler.post(new Runnable()
{
#Override
public void run()
{
chat.setText(msg);
}
});
out.writeBytes(str);
out.flush();
out.close();
client.close();
}catch(IOException e){}
}
}
public class serverThread implements Runnable {
#Override
public void run() {
try {
while (true) {
serverSocket = new ServerSocket(serverport);
client = serverSocket.accept();
handler.post(new Runnable() {
#Override
public void run() {
display_status.setText("Connected");
}
});
/*******************************************
setup i/p streams
******************************************/
in = new DataInputStream(client.getInputStream());
String line = in.readUTF();
while (!line.equals("STOP")) {
msg = msg + "\n Client : " + line;
handler.post(new Runnable() {
#Override
public void run() {
chat.setText(msg);
}
});
}
in.close();
client.close();
}
} catch (Exception e) {
}
}
}
}
//#Client Side Java File
package reciever;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.*;
import java.net.*;
import inc.teckzy.wif_chat.R;
public class ClientSide extends AppCompatActivity {
EditText serverIp,smessage;
TextView chat;
String serverIpAddress = "",msg = "",str;
Handler handler = new Handler();
Button sent,connectPhones;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_side);
chat = (TextView) findViewById(R.id.chat);
serverIp = (EditText) findViewById(R.id.server_ip);
smessage = (EditText) findViewById(R.id.smessage);
sent = (Button) findViewById(R.id.sent_button);
connectPhones = (Button) findViewById(R.id.connect_phones);
//-----------------------Connecting to IP------------------------------//
connectPhones.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(""))
{
Thread clientThread = new Thread(new
ClientThread());
clientThread.start();
}
}
});
//-------------------------------Initializing sent thread----------------//
sent.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Thread sentThread = new Thread(new sentMessage());
sentThread.start();
}
});
}
class sentMessage implements Runnable
{
#Override
public void run()
{
try
{
InetAddress serverAddr =
InetAddress.getByName(serverIpAddress);
Socket socket = new Socket(serverAddr, 6666); //
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
str = smessage.getText().toString();
str = str + "\n";
msg = msg + "Client : " + str;
handler.post(new Runnable() {
#Override
public void run() {
chat.setText(msg);
}
});
os.writeUTF(str);
os.flush();
os.close();
socket.close();
}
catch(IOException e)
{
}
}
}
public class ClientThread implements Runnable
{
InetAddress serverAddr;
public void run()
{
try
{
while(true)
{
serverAddr = InetAddress.getByName(serverIpAddress);
Socket socket = new Socket(serverAddr, 6666);
/*******************************************
setup i/p streams
******************************************/
DataInputStream in = new
DataInputStream(socket.getInputStream());
String line = in.readUTF();
while (!line.equals("Stop"))
{
msg = msg + "Server : " + line + "\n";
handler.post(new Runnable()
{
#Override
public void run()
{
chat.setText(msg);
}
});
}
in.close();
socket.close();
Thread.sleep(100);
}
}
catch (Exception e)
{}
}
}
}
You are using client = serverSocket.accept(); in server side every time you need to send a message.
And in client side you are trying to create a new Socket connection using new Socket
(serverAddr, 6666); multiple times.
Maintain a single Socket connection and single data input/output stream(do not create multiple instances) and you will be good to go.
See example at:
https://github.com/nabinbhandari/android-socket-messaging
I Solved it Just deleted the "sentMessage" Thread and implemented output stream in "sent.setOnClickListener(new View.OnClickListener()" in both Activity also did some modifications..Here is the working code Activity ...Hope this will help someone
#Server Activity
package inc.teckzy.wifi_chat;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Helper extends AppCompatActivity {
EditText smessage;
TextView chat,display_status;
String str,Smsg,Cmsg="";
int serverport = 6666;
ServerSocket serverSocket;
Socket client;
Handler updateConversationHandler =new Handler();
WifiManager wmanager;
DataOutputStream out;
DataInputStream in;
Button button_sent;
StringWriter errors = new StringWriter();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helper);
wmanager = (WifiManager) getSystemService(WIFI_SERVICE);
#SuppressWarnings("deprecation")
String ip = Formatter.formatIpAddress(wmanager.getConnectionInfo().getIpAddress());
smessage = (EditText) findViewById(R.id.smessage);
chat = (TextView) findViewById(R.id.chat);
display_status = (TextView) findViewById(R.id.display_status);
Thread serverThread = new Thread(new serverThread());
serverThread.start();
display_status.setText("Hosted on: " + ip);
button_sent = (Button) findViewById(R.id.button_sent);
button_sent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
out = new DataOutputStream(client.getOutputStream());
str = smessage.getText().toString();
Smsg = "Server:" + str;
updateConversationHandler.post(new updateUIThread(Smsg));
out.writeUTF(str);
out.flush();
}catch (Exception e) {
e.printStackTrace(new PrintWriter(errors));
updateConversationHandler.post(new updateUIThread(errors.toString()));
}
smessage.getText().clear();
}
});
}
public class serverThread implements Runnable {
#Override
public void run() {
try {
serverSocket = new ServerSocket(serverport);
while (true) {
client = serverSocket.accept();
updateConversationHandler.post(new updateUIThread("Connected"));
/*******************************************
setup i/p streams
******************************************/
in = new DataInputStream(client.getInputStream());
String line = "";
while (!line.equals("STOP")) {
line=in.readUTF();
Cmsg ="Client: "+ line;
updateConversationHandler.post(new updateUIThread(Cmsg));
}
in.close();
client.close();
}
} catch (Exception e) {
e.printStackTrace(new PrintWriter(errors));
updateConversationHandler.post(new updateUIThread(errors.toString()));
}
}
}
class updateUIThread implements Runnable {
private String msg;
updateUIThread(String str) {
this.msg = str;
}
#Override
public void run() {
chat.setText(chat.getText().toString()+ msg + "\n");
}
}
#Override
protected void onStop() {
super.onStop();
try {
// MAKE SURE YOU CLOSE THE SOCKET UPON EXITING
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And
#Client Activity
package inc.teckzy.wifi_chat.reciever;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.Socket;
import inc.teckzy.wifi_chat.R;
public class ClientSide extends AppCompatActivity {
EditText serverIp,smessage;
TextView chat;
String serverIpAddress = "",Smsg,Cmsg = "",str;
Handler updateConversationHandler = new Handler();
Button sent,connectPhones;
Socket socket;
DataInputStream in;
DataOutputStream out;
StringWriter errors = new StringWriter();
WifiManager wmanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_side);
chat = (TextView) findViewById(R.id.chat);
serverIp = (EditText) findViewById(R.id.server_ip);
wmanager = (WifiManager) getSystemService(WIFI_SERVICE);
smessage = (EditText) findViewById(R.id.smessage);
sent = (Button) findViewById(R.id.sent_button);
connectPhones = (Button) findViewById(R.id.connect_phones);
//-----------------------Connecting to IP------------------------------//
connectPhones.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals(""))
{
Thread clientThread = new Thread(new
ClientThread());
clientThread.start();
}
}
});
//-------------------------------Initializing sent thread----------------//
sent.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
try {
out = new DataOutputStream(socket.getOutputStream());
str = smessage.getText().toString();
Cmsg = "Client: " + str;
updateConversationHandler.post(new updateUIThread(Cmsg));
out.writeUTF(str);
out.flush();
}catch(Exception e){
e.printStackTrace(new PrintWriter(errors));
updateConversationHandler.post(new updateUIThread(errors.toString()));}
smessage.getText().clear();
}
});
}
public class ClientThread implements Runnable
{
InetAddress serverAddr;
public void run()
{
try
{
serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 6666);
updateConversationHandler.post(new updateUIThread("Connected"));
String ip = socket.getRemoteSocketAddress().toString();
updateConversationHandler.post(new updateUIThread(ip));
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
/*******************************************
setup i/p streams
******************************************/
String line = "";
while (!line.equals("Stop")) {
line=in.readUTF();
Smsg ="Server: " + line + "\n";
updateConversationHandler.post(new updateUIThread(Smsg));
}
out.close();
in.close();
socket.close();
}
catch (IOException e)
{updateConversationHandler.post(new updateUIThread("IO Error"));}
}
}
class updateUIThread implements Runnable {
private String msg;
updateUIThread(String str) {
this.msg = str;
}
#Override
public void run() {
chat.setText(chat.getText().toString()+ msg + "\n");
}
}
#Override
protected void onStop() {
super.onStop();
try {
// MAKE SURE YOU CLOSE THE SOCKET UPON EXITING
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
i started with android a short time ago. This time i tried to connect a c# tcp server with a java tcp client. To debug i worte a c# client and server and if i use the c# client to talk to the server, there is no problem. But my java socket has problems with connecting. I copied the java tcp code for example from the net.
c# code:
namespace MTCliserv2
{
class MTCliserv2
{
static void Main(string[] args)
{
string txt;
Console.Write("MTCliserv2 Server or Client (s/c): ");
txt = Console.ReadLine();
if (txt == "s")
Server();
else
Client();
Console.Write("\nHit any Key to continue "); Console.ReadKey();
}
static IPAddress GetIPAddress()
{
IPAddress ipAdr = Dns.Resolve("localhost").AddressList[0];
ipAdr = IPAddress.Parse("192.168.1.1");
return ipAdr;
}
static void Server()
{
TcpListener server;
Socket socke;
IPAddress ipAdr = GetIPAddress();
try
{
server = new TcpListener(ipAdr, 13);
server.Start();
Console.WriteLine("Server {0} gestartet", server.LocalEndpoint);
while (true)
{
socke = server.AcceptSocket(); // Aufruf blockiert!!
mit diesem Client abwickelt
new ConnTalk(socke);
}
}
catch (Exception e)
{
Console.WriteLine("Error {0}", e);
}
}
static void PrintConnectionInfo(Socket aSock)
{
Console.WriteLine("Anfrage von {0}", aSock.RemoteEndPoint);
}
static void Client()
{
const string serverName = "localhost";
try
{
TcpClient client = new TcpClient("192.168.1.1", 13);
NetworkStream strm = client.GetStream();
Socket sc = client.Client;
StreamReader strmRd = new StreamReader(strm);
StreamWriter strmWr = new StreamWriter(strm);
string txt, txt2;
Console.WriteLine("Connected to {0}", sc.RemoteEndPoint);
while (true)
{
Console.Write("Msg= ");
txt = Console.ReadLine();
if (txt == "end")
break;
strmWr.WriteLine(txt); strmWr.Flush();
txt2 = strmRd.ReadLine();
Console.WriteLine("Answer: {0}", txt2);
}
client.Close(); strmRd.Close(); strmWr.Close();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
// Pro Verbindung wird 1 ConnTalk-objekt erzeugt
class ConnTalk
{
Thread m_Thr;
Socket m_Sc;
public ConnTalk(Socket aSc)
{
m_Sc = aSc;
m_Thr = new Thread(this.TalkWithClient);
m_Thr.Start();
}
void TalkWithClient()
{
NetworkStream strm = new NetworkStream(m_Sc);
StreamReader strmRd = new StreamReader(strm);
StreamWriter strmWr = new StreamWriter(strm);
string txt;
Console.WriteLine("Talking with {0}", m_Sc.RemoteEndPoint);
try
{
while (true)
{
txt = strmRd.ReadLine();
Console.WriteLine("Msg: {0}", txt);
txt += " Echo!! \r\n";
strmWr.Write(txt); strmWr.Flush();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception in TalkWithClient");
}
Console.WriteLine("{0} Disconnected", m_Sc.RemoteEndPoint);
m_Sc.Close(); strm.Close(); strmRd.Close(); strmWr.Close();
return;
}
}
}
And here is the java code:
package com.example.mogri.tcpsocket;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ettext;
private TextView tvlog;
TcpClient mTcpClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET},225);
}
if ( ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_NETWORK_STATE},225);
}
ettext = (EditText) findViewById(R.id.etText);
tvlog = (TextView) findViewById(R.id.tVlog);
Button btnsend = (Button) findViewById(R.id.btnsend);
btnsend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String input = ettext.getText().toString();
mTcpClient.sendMessage(input);
log(input);
}
});
new ConnectTask().execute("");
}
public void log(String add){
tvlog.setText( tvlog.getText()+"\n"+add);
}
public class ConnectTask extends AsyncTask<String, String, TcpClient> {
#Override
protected TcpClient doInBackground(String... message) {
//we create a TCPClient object
mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
#Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
}
});
mTcpClient.run();
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//response received from server
Log.d("test", "response " + values[0]);
//process server response here....
}
}
}
The tcpclient class:
package com.example.mogri.tcpsocket;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
/**
* Created by mogri on 01.04.2017.
*/
public class TcpClient {
public static final String SERVER_IP = "192.168.1.1"; //server IP address
public static final int SERVER_PORT = 13;
// message to send to the server
private String mServerMessage;
// sends message received notifications
private OnMessageReceived mMessageListener = null;
// while this is true, the server will continue running
private boolean mRun = false;
// used to send messages
private PrintWriter mBufferOut;
// used to read messages from the server
private BufferedReader mBufferIn;
public TcpClient(OnMessageReceived listener) {
mMessageListener = listener;
}
public void sendMessage(String message) {
if (mBufferOut != null && !mBufferOut.checkError()) {
mBufferOut.println(message);
mBufferOut.flush();
}
}
/**
* Close the connection and release the members
*/
public void stopClient() {
mRun = false;
if (mBufferOut != null) {
mBufferOut.flush();
mBufferOut.close();
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVER_PORT);
try {
//sends the message to the server
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
//receives the message which the server sends back
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(mServerMessage);
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
I need my server to keep track of each of it's client's connection. I've been advised to use Threads. So what I'm trying to achieve is to create a Thread for each client, which should run till the client connection exists. But what is happening is that for each message any client sends, a new client connection gets created in the doInBackground() function. So instead of having one single thread for one single client, I'm getting one thread for any client message sent to the server. Can you suggest a method in with which my server would be able to distinguish different messages sent from different clients?
Java Server Code :
package com.nss.academyassistserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class AcademyAssistServer {
public static ServerSocket serverSocket;
public static Socket clientSocket;
static final int PORT = 4444;
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
serverSocket = new ServerSocket(PORT); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: "+PORT+" \n");
}
System.out.println("Server started. Listening to the port "+PORT);
while (true) {
try {
clientSocket = serverSocket.accept();
System.out.println("New connection accepted."); // accept the client connection
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
//new thread for a client
new EchoThread(clientSocket).start();
}
}
}
class EchoThread extends Thread {
InputStreamReader inputStreamReader;
BufferedReader bufferedReader;
String fromClient;
Socket clientSocket;
public EchoThread(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
try {
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
} catch (IOException e) {
return;
}
while (!Thread.currentThread().isInterrupted()) {
System.out.println("I am thread " + Thread.currentThread().getId());
try {
fromClient = bufferedReader.readLine();
if ((fromClient == null) || fromClient.equalsIgnoreCase("exit")) {
System.out.println("You're welcome, bye!");
return;
} else {
System.out.println(fromClient);
Thread.currentThread().interrupt();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client Activity Code:
package com.nss.academyassist;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
//import statements for client
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
//import statements for client
import android.os.AsyncTask;
public class MainActivity extends Activity implements OnClickListener {
EditText question;
//Client sockets
private Socket client;
private PrintWriter printwriter;
private String toTag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
question = (EditText)findViewById(R.id.editText1);
Button query = (Button)findViewById(R.id.button2);
query.setOnClickListener(this);
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("Server IP Address", 4444);
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(toTag); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button2:
toTag = question.getText().toString();
// Output the result
Toast.makeText(getApplicationContext(), toTag,
Toast.LENGTH_LONG).show();
//Invoke the execute method of AsynTask, which will run the doInBackground method of SendMessage Class
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
break;
}
}
}
You can use IP to tell the clients. Use
clientSocket.getInetAddress().getHostAddress()
You can also keep the client not closed in the android code. For example, you may open the Socket in onCreate and close it in onDestroy.
The cause for your problem is in your client code. Using new Socket(..) your client will create a new connection each time it sends a tag to the the server. So instead of that you could create a single connection that is reused:
public class MainActivity extends Activity implements OnClickListener {
/* .. your other variables .. */
private Socket client;
private PrintWriter printwriter;
#Override
protected void onCreate(Bundle savedInstanceState) {
/* .. no change here .. */
}
public void onStart()
{
if(this.client != null)
{
try {
client = new Socket("Server IP Address", 4444);
printwriter = new PrintWriter(client.getOutputStream(), true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void onClose()
{
this.printwriter.close();
this.printwriter = null;
this.client.close();
this.client = null;
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
printwriter.write(toTag); // write the message to output stream
printwriter.write("\n"); // delimiter
printwriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button2:
toTag = question.getText().toString();
// Output the result
Toast.makeText(getApplicationContext(), toTag,
Toast.LENGTH_LONG).show();
//Invoke the execute method of AsynTask, which will run the doInBackground method of SendMessage Class
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
break;
}
}
}
In addition to that you should append some delimiter to your tag/message in order for the server to be able to distinguish the content from different messages.
Since you are using BufferedReader.readLine() which seperates lines
by any one of a line feed ('\n'), a carriage return ('\r'), or a
carriage return followed immediately by a linefeed
I added a line that appends a line feed after the tag in the example above for that purpose.
i have my code that sucessfuly send my client message to server.. can anyone help pls ? i'm new in socket programming. i want to establish a server that sends the message of my android client to other android client... here is my code
CLIENT ANDROID :
package com.example.websocketclient;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Chatroom extends Activity {
public static String msgToServer = null;
TextView uname;
EditText in;
Button snd;
TextView out;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatroom);
uname = (TextView) findViewById(R.id.textViewmynam);
in = (EditText) findViewById(R.id.editTextInput);
snd = (Button) findViewById(R.id.buttonSend);
out = (TextView) findViewById(R.id.textViewOutput);
Bundle bundle = getIntent().getExtras();
String urname = bundle.getString("myname");
uname.setText(urname);
MyClientTask clientTask = new MyClientTask();
clientTask.execute();
}
public class MyClientTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
connect();
return null;
}
}
public void connect() {
Socket sock;
int[] allports = {1111,1919,2020};
int portnum;
for(int i=1;i<allports.length;i++) {
try {
portnum = allports[i];
boolean socketno = Middleware.available(portnum);
String ad = "192.168.149.1";
if(socketno = true) {
sock = new Socket(ad,portnum);
while(socketno) {
sendmsg(sock);
}//while
// sock.close();
}//if
}catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendmsg (final Socket s) {
snd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//boolean listening = true;
String inmsg = ""+in.getText().toString();
String response = "";
msgToServer = inmsg;
//out.append("you:"+msgToServer+"\n");
try {
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());
DataInputStream inToServer = new DataInputStream(s.getInputStream());
if(outToServer != null){
outToServer.writeBytes(msgToServer+"\n");
}
}
catch (Exception e) {
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chatroom, menu);
return true;
}
}
Here is my PC Server side :
package Server;
import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
public class SocketServer extends Thread {
final static int _portNumber = 1919; //Arbitrary port number
public static void main(String[] args)
{
try {
new SocketServer().startServer();
} catch (Exception e) {
System.out.println("I/O failure: " + e.getMessage());
}
}
public void startServer() throws Exception {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(_portNumber);
} catch (IOException e) {
System.err.println("Could not listen on port: " + _portNumber);
System.exit(-1);
}
while (listening) {
System.out.println("Huwat huwat kang client..!");
handleClientRequest(serverSocket);
}
serverSocket.close();
}
private void handleClientRequest(ServerSocket serverSocket) {
try {
new Thread(new ConnectionRequestHandler(serverSocket.accept())).start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Handles client connection requests.
*/
public class ConnectionRequestHandler implements Runnable{
private Socket _socket;
private PrintWriter _out;
private BufferedReader _in;
public ConnectionRequestHandler(Socket socket) {
_socket = socket;
}
public void run() {
String info;
int maxid;
System.out.println("Client connected to socket: " + _socket.toString());
info = _socket.toString();
//incrementing the msg_id
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
_socket.getOutputStream()));
_in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
String inputLine;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/finalclient","root","");
PreparedStatement st = con.prepareStatement("select *from msgs order by msg_id desc");
ResultSet r1 = st.executeQuery();
r1.first(); // or r1.next()
maxid = r1.getInt("msg_id") + 1;
while ((inputLine = _in.readLine()) != null) {
System.out.println("Message from Client: "+inputLine);
out.write(inputLine);
try {
PreparedStatement st1=con.prepareStatement("insert into msgs values(?,?,?)");
st1.setInt(1,maxid);
st1.setString(2,info);
st1.setString(3,inputLine);
int count = st1.executeUpdate ();
System.out.println (count + " rows were inserted to Database");
}
catch (Exception e) {
System.out.println("Failed to Insert from DB"+e);
}
}
}
catch (Exception e) {
System.out.println("Failed to Select from DB"+e);
}
}
}
}