Socket server, I don't receive the message sent by my Server - java

I Have a client-Server connection..
Whem my server is connected to the client it must send a message through the outputstream and then print it when it receive the message from the inputStream. But when the client is connected I receive back the "Connected" message but I don't see the "this is a Text message\n" sent my the Server
This is my Server:
public class Server1 extends JFrame{
static JTextArea testoarea;
static Socket socket;
static ServerSocket server;
public Server1(){
testoarea = new JTextArea();
add(new JScrollPane(testoarea));
setSize(600, 700);
setVisible(true);
}
public static void main(String[] args) {
Server1 server1 = new Server1();
server1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inizzializza();
connetti();
}
private static void inizzializza(){
try {
server = new ServerSocket(7100);
showMessage("Server Iniziato\n");
} catch (IOException e) {
System.out.println(e);
}
}
private static void connetti() {
try {
socket = server.accept();
showMessage("Connected \n");
PrintWriter output = new PrintWriter(socket.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.println("this is a Test message\n");
showMessage(input.readLine().toString());
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void showMessage(final String text) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
testoarea.append(text);
}
}
);
}
}
this is what I get back:

Your input.readLine() is on your server side client input stream. That means it tries to read an income message from the client.
If you wan't read the message send by the server to the client from the client side (what I think you want, but I maybe misunderstood). You have to get the client socket input stream.
The socket you get where you have probably call somewhere:
Socket clientSocket = new Socket("localhost", 7100);

Related

Can I use a list of sockets to send to multiple clients?

I have a ArrayList<Socket> listOfSockets that adds a socket when I receive clientsocket = serversocket.accept() like this listOfSockets.add(clientsocket)
and then I send this whole list to another class like this.
Server_Client client = new Server_Client(clientSock, clientSocketList);
Thread X = new Thread(client);
X.start();
Here is the code for my class that I send the list and socket to
public class Server_Client implements Runnable{
//ArrayList<String> UserNameList;
ArrayList<Socket> clientSocketList;
Socket socket = null;
public Server_Client(Socket X, ArrayList<Socket> L){
this.socket = X;
this.clientSocketList = L;
}
#Override
public void run() {
try {
ListenforMessage NewMessages = new ListenforMessage(socket);
Thread myThread = new Thread(NewMessages);
myThread.start();
} catch (Exception e) {
System.err.println(e);
}
}
//------------------------------------------------------------------
//Class that handles incoming messages!
class ListenforMessage implements Runnable{
Socket socket;
DataInputStream IN;
DataOutputStream OUT;
public ListenforMessage(Socket x) {
this.socket = x;
}
#Override
public void run() {
try {
while (true) {
IN = new DataInputStream(socket.getInputStream());
OUT = new DataOutputStream(socket.getOutputStream());
String message = IN.readUTF();
//System.out.println(message);
for (Socket TEMP_SOCK : clientSocketList) {
if(TEMP_SOCK != this.socket){
SendMessage(message);
}
}
}
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void SendMessage(String m) throws IOException{
try {
DataOutputStream OUT = new DataOutputStream(socket.getOutputStream());
OUT.writeUTF(m);
System.out.println("User said: " + m);
} catch (Exception e) {
}
}
//------------------------------------------------------------------
}
In the ListenforMessage class, in the for-loop I go trough the size of list and if TEMP_SOCK is not this.socket then I want to send a message to that socket, and if it is then don't do anything. But my problem is that when I connect with 2 clients they can't send message to each other. They just send the message to the server and the server sends back the message to the client. My question is, can you use a list like I do to check if the socket is not the socket that you are YOURSELF.
cause now Client1 sends a messages and receive the same messages from sever, and Client2 does the same thing.
I want Client1 to send and Client2 to receive the message(And other sockets on the same list)
Your code doesn't work because you should let SendMessage know to which socket it should send the message (i.e. you should pass TEMP_SOCK to it), instead, you're making it use the socket variable, which is the socket connected to the client that just sent the message.

Send message to server java

I'm creating chat program. At first, I send message to server from client and server should get message, but the message is not sent to the server until I shut down client.
Here is my code:
Client:
private Socket Client;
private Thread RunClient;
private JButton Send;
private int Port=8000;
private String Host="localhost";
public void init() {//Here we connecting to server
Send=new JButton();
Send.setFont(new Font("Times New Roman", Font.BOLD, 15));
Send.setLocation(575,Text.getHeight()+15);//395
Send.SetBorderColor(Color.cyan);
Send.setForeground(Color.white);
Send.setText("Send");
Send.setSize(30,70);
Send.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
SendButton_Click(e);
}
});
try
{
InetAddress address = InetAddress.getByName(Host);
Client = new Socket(address, Port);
System.out.println("Client started. Port: "+Port+"\n");
}catch(Exception e){
System.out.println("Error:"+e);
}
}
public void SendButton_Click(ActionEvent e) {//Here we sending message to server
String sendMessage = "hi";
try {
sendMessageToServer(Client,sendMessage);//sending message to server
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Message sent to the server : "+sendMessage);
}
}
private void sendMessageToServer(Socket socket,String message) throws IOException {
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
writer.write(message);
writer.flush();
}
Here is server:
private int Port=8000;
public void init() {//Here we starting server and starting thread
try {
serverSocket = new ServerSocket(Port);
System.out.println("Server on. Port: "+Port+"\n");
} catch (IOException e1) {
e1.printStackTrace();
}
RunServer = new Thread() {
public void run() {
ServerLoop();
}
};
RunServer.start();
}
public void ServerLoop() {//here we should receive message..
while(true) {
System.out.println("Server loop");
try
{
Socket clientSocket = serverSocket.accept();
InputStreamReader inputstreamreader = new
InputStreamReader(clientSocket.getInputStream());
BufferedReader bufferedreader = new
BufferedReader(inputstreamreader);
PrintWriter printwriter = new
PrintWriter(clientSocket.getOutputStream(),true);
String line = "";
boolean done = false;
while (((line = bufferedreader.readLine()) != null) &&(!done)){
System.out.println("Received from Client: " + line);
if (line.compareToIgnoreCase("Exit") == 0) done = true;
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
Your server is waiting for a full line, i.e. a String that is terminated with \r, \n, or both. Your client does not send a line ending, thus the server's call to readLine() cannot complete until the stream ends, when the remaining (non-line-terminated) input is returned.
Try sending "hi\n" and it should work.
The readLine() method will wait until it receives a newline character (\n) before returning. This means that the method blocks until the client is disconnected. The solution is simple; change the line in sendMessageToServer() that is
writer.write(message);
to
writer.write(message + "\n");
However, you will not be able to send the server messages containing \n without bugs. To do so, you will have to rewrite your reading code to use character-by-character reading.

Java server client networking

Should I open - close the connection each time my client GUI sends a message or open a connection one time and keep it open ?
Im sending data using BufferedWriter but this methid just seems to work for one time only, after doing the write and the flush method Im able just to send 1 message to my server, then it just doesnt get the data im sending...
Client connects - sends data to server (1st time) - Works
Client sends data again - no data echoed on server console.
Should I open a bufferedwriter each time I want to send data or keep one open all the time and never close it?
My actual code - Client side
Main
public class Main {
public static void main(String args[]) {
Client chat = new Client("localhost", 6111);
}
}
Client
public class Client {
public Client(String host, int port) {
SwingUtilities.invokeLater(new Runnable() {
private Socket chat;
public void run() {
try {
this.chat = new Socket("localhost", 6113);
} catch ( IOException e) {
e.printStackTrace();
}
Gui gui = new Gui("Chat", this.chat);
}
});
}
}
GUI
public class Gui {
private JFrame frame;
private JTextArea area;
private JTextField field;
private JMenuBar menu;
private JButton button;
private Socket socket;
private BufferedWriter write;
public Gui(String title, Socket socket) {
this.socket = socket;
try {
this.write = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
this.frame = new JFrame(title);
this.frame.setSize(new Dimension(400, 400));
this.frame.setLayout(new BorderLayout());
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setResizable(false);
Elements interfce = new Elements(this.frame);
this.field = interfce.addField("Texto to send...");
this.menu = interfce.addBar();
this.area = interfce.addArea();
this.button = interfce.addButton("Send");
this.button.addActionListener(new Listener(this.field, this.socket, this.write));
this.menu.add(this.field);
this.menu.add(this.button);
this.frame.setVisible(true);
}
}
Listener (sends data)
public class Listener implements ActionListener {
private JTextField text;
private Socket chat;
private BufferedWriter writer;
public Listener(JTextField field, Socket chat, BufferedWriter write) {
this.text = field;
this.chat = chat;
this.writer = write;
}
public void actionPerformed(ActionEvent e) {
System.out.println(this.text.getText());
try {
writer.write("Hello\n");
writer.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
And the server
public class Main {
public static void main(String args[]) {
Server chat = new Server(6113);
}
}
public class Server {
private int port;
private ServerSocket server;
public Server(int port) {
this.port = port;
startServer();
}
public void startServer() {
try {
this.server = new ServerSocket(this.port);
System.out.println("Starting chat server at port " + this.port + "...");
while (true) {
Socket s = this.server.accept();
System.out.println("Client connected - " + s.getLocalAddress().getHostName());
Thread client = new Thread(new Client(s));
client.start();
}
} catch (IOException e) {
System.out.println("Error found!");
e.printStackTrace();
}
}
}
public class Client implements Runnable {
private Socket client;
public Client(Socket client) {
this.client = client;
}
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Should I open - close the connection each time my client GUI sends a message or open a connection one time and keep it open ?
Connection between a Client and Server is generally open for the entire session of messaging and not recreated for sending each message. So keep it open until the client or the Server has to go away.
Look at your Client thread that you start in your server upon accepting a Connection.
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
System.out.println("Client says - " + read.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
Does this give you a hint ? Well - you have to arrange for some sort of looping here within your run() - listening for more inputs from that client.
Just do some changes
Server side:
public void run() {
try {
BufferedReader read = new BufferedReader(new InputStreamReader(
this.client.getInputStream()));
while (true) {
System.out.println("Client says - " + read.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
Client side:
use PrintWriter instead of BufferedWriter that provide print new line functionality and auto flush property.
this.write = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()),
true);
....
public void actionPerformed(ActionEvent e) {
System.out.println(this.text.getText());
try {
writer.write(this.text.getText());
writer.println();
} catch (Exception e1) {
e1.printStackTrace();
}
}

TCP read a file and send to the client in java

Let me explain my work.
I have to send a file from server to client. In server I accepted the connection from the client and not closed the connection. Now I have to send the data as streams to the client.
Now I have selected a content and wrote in a file in method.
I have send method in which I have to send the file in inputstreams to the client. How to send?
public static void main(String args[])
{
int port=5000;
while(true)
{
try
{
ServerSocket ser=new ServerSocket(port+10);
System.out.println("CLIENT A IS CONNECTED");
ser.close();
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser1=new ServerSocket(port+20);
ser1.accept();
System.out.println("CLIENT B IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser2=new ServerSocket(port+30);
ser2.accept();
System.out.println("CLIENT C IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser3=new ServerSocket(port+40);
ser3.accept();
System.out.println("CLIENT D IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
In main method I accepted All the client request.
private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) //sendbutton
{
try
{
FileReader buf=new FileReader("e:\\input.txt");
int port= // the port of client which I selected to send the data
try
{
#SuppressWarnings("resource")
ServerSocket ser=new ServerSocket(port);
Socket soc=ser.accept();
BufferedReader toclient=new BufferedReader(buf);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
System.out.println(dos.toString());
dos.flush();
dos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
Now my question is I already opened the ports and the connection is established in the main method. I have to send the data from server to client by selecting to which client should receive the data in sendbutton method. I am confused how to check or pass the socket object serv1 to the send method?.
below is the sample server code (please add corresponding import statements and your logic)
class SampleServer
{
private int port;
private ArrayList clientList;
public SampleServer()
{
this.port = 4444;
}
public SampleServer(int port)
{
this.port = port;
}
public void startServer()
{
ServerSocket ss = new ServerSocket(this.port);
Socket soc;
while(true)
{
soc = ss.accept();
clientList.add(soc);
}
}
public ArrayList getClientList()
{
return clientList;
}
}
Below is the sample main method (take this as a reference and create your own code)
public class Main
{
public static void main(String[] args) throws Exception
{
//Create a server
//Server has to be only one and multiple clients can connect it
SampleServer server = new SampleServer(5555);
server.startServer();
//Creating one client
Socket soc = new Socket("localhost", 5555);
//Similarly create n-number of clients and connect to the same port as that of server
//server.getClientList(); will help you get the list of the clients connected to the server
}
}

serversocket server + client issues

ok is hard for me to describe my problem now but then i will try my best to in order for me to get some assist.
technically i have a server.java and client.java as a super class. and my layout structure for my server and client connection goes like this
MAIN SERVER --- CLIENT/SERVER ----- CLIENT
my main problem is the this CLIENT/SERVER part is 1 driver class that calls 2 different classes which is CLIENT and SERVER together... and this creates a problem when my CLIENT sends something that needs to be received by MAIN SERVER side needs to go through CLIENT/SERVER part. if is that condition happens..
the CLIENT of course need to interact with CLIENT/SERVER (SERVER) part because is a SERVER that accepts the CLIENT data. but now i wanted the (SERVER) part in the CLIENT/SERVER to transfer the data to (CLIENT) in the CLIENT/SERVER part so that it can be send to the MAIN SERVER
how is it possible for me to write something that allows the CLIENT/SERVER to interact with each other so it can transfer the data between them vise versa? how ever this is my code for calling the CLIENT and SERVER together
public class Slave {
public static void main(String args []) throws IOException{
try{
// set Config file settings to slave mode
Config cfg = new Config("Slave");
String MasterServerIP = cfg.getProperty("ServerIP");
String MasterServerPort = cfg.getProperty("ServerPort");
String SlaveServerPort = cfg.getProperty("ListeningPort");
System.out.println("Slave Client connecting to Master Server");
// start connect to master server by calling the SlaveClient class
new SlaveClient(MasterServerIP,Integer.parseInt(MasterServerPort)).start();
int numClient = 0;
ServerSocket listener = new ServerSocket(Integer.parseInt(SlaveServerPort));
System.out.println("Server starts running");
try{
while(true){
// start listening to the server port by calling SlaveServer class
new SlaveServer(listener.accept(), numClient++, Integer.parseInt(SlaveServerPort)).start();
}
} finally {
listener.close();
}
} catch (FileNotFoundException file) {
System.out.println("File Not Found Error: "+file.getMessage());
}
}
}
the above is only the driver class that calls the 2 object class which is the SERVER and CLIENT side.
i will attach my slaveserver and slaveclient code here but i am not sure how to do it like you said
public class SlaveServer extends Server {
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();
// SlaveServer Constructor
public SlaveServer(Socket socket, int numClient, int port) {
super(socket, numClient, port);
}
public void writeToMsg(String msg){
msgArea.append(msg+"\n");
}
public void writeToConnection(String msg){
connectionArea.append(msg+"\n");
}
public void run(){
try{
startGUI();
// initial BufferedReader and PrintWriter object by binding it with Socket
BufferedReader in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
PrintWriter out = new PrintWriter(getSocket().getOutputStream(), true);
// welcome message send from server to client
out.println("Welcome to the Slave Server port:"+getPort()+" client #"+getNumClient());
while(true){
String readmsg = in.readLine();
writeToMsg(readmsg);
}
} catch (IOException e){
writeToMsg("Error in closing Socket");
}
writeToConnection("Connection from client #"+getNumClient()+" is closed");
}
}
public class SlaveClient extends Client{
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame();
private JTextArea msgArea = new JTextArea();
private JTextArea connectionArea = new JTextArea();
// SlaveClient Constructor
public SlaveClient(String ip, int port) {
super(ip, port);
}
public void run(){
startGUI();
Socket sock = null;
try {
sock = new Socket(getIp(), getPort());
} catch (IOException e) {
e.printStackTrace();
}
try {
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
try {
out = new PrintWriter(sock.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
out.println("TEST");
// while loop for reading message from server
while(true){
try {
getMsg(in);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
So you are trying to write a proxy?
You you need to give the server half of the proxy a reference to the client half, so that it can forward the data.
Then create a method in the client half to accept messages from the server half.
So each message you read in at the server half, you pass to the client half. The client half can then pass it to the real server.

Categories

Resources