0

I'm making a simple network using Socket.

It works fine but the problem is it's working like a board game

Every time the server has to wait for the client then the client will wait for the server and so on.

I want the data to be sent from server to client and from client to server whenever I enter data from any side.

Here is a part of my code in server

in = Integer.parseInt(myInputStream.readLine())); // server gets data
out = new Scanner(System.in).nextInt(); 
myOutputStream.println(column);  // server sends data
4
  • If I understand correctly, the server should also be a client and the client should also be the server? So basically you require 2 connections to be made. One from A to B and one from B to A. Commented Jun 9, 2011 at 13:00
  • Hi there, somehow I can't get what yo are trying to tell. Please provide with more code or explain it once more deeply. When opening a Socket, the call (accept or receive) will block and wait for an incoming connection (if ServerSocket) or an incoming datagram (if DatagramSocket). So in that case "waiting" is okay with me. (download.oracle.com/javase/tutorial/networking/sockets/…) Commented Jun 9, 2011 at 13:00
  • @THelper is it possible ? theoretically at least? Commented Jun 9, 2011 at 13:03
  • @nyyrikki simply i don't want anyside to wait for the other side data... Commented Jun 9, 2011 at 13:05

3 Answers 3

2

In a single word: threads. Your application needs multiple threads on each end. In particular, there should be threads devoted to maintaining queues of incoming and outgoing messages on each end, so that code wanting to send or receive a message does not have to wait.

This is a very big topic -- I can't really show you exactly what to do. I'd recommend the Concurrency chapter of the Java Tutorial to get started.

Sign up to request clarification or add additional context in comments.

4 Comments

Have the "send" part of your socket be in your main class. Then you can send whenever you want e.g. push of a button. Have the receive part in a while-loop in a separate thread - this will wait for incoming messages on its own time and not affect the rest.
Well, no, not really. If you send synchronously, then you'll need to wait to send if the network is slow. Better to do both in dedicated threads.
True what you say there. But sending a simple String across a LAN will not take more than a second. I have created many LAN chat-rooms/games/applications which sent the stuff immediately, even if I am busy to copy files across the network. But it is still true what you said Ernest! ... I created an application once that sent a screenshot every half-a-sec and no lagging/delay occurred.
i made 2 threads one to read and another to write but it gives me exception when i run ... <br> check them <br> pastebin.com/mEsLKAH4<br> pastebin.com/KCqenKnT <br> pastebin.com/0YLDn9AU
0

i want the data to be sent from server to client and from client to server whenever i enter data from any side..

In order to achieve the above, you need both your client and server to be multi-threaded. So, while one thread on each side listens to data coming in from the other peer, the processing of the message is actually done by another thread.

If you haven't done multi-threaded programming in any language before this might be a daunting task, so following is a link to start with:

Multithreaded Client/Server Applications

Then you can go to the following link for better ideas of structuring your game:

Network Programming Example: A Networked Game Framework

Comments

0
Server.java


import java.io.*;

import java.net.*;

public class Server implements Runnable {

String messageIN, messageOUT;
Thread t1 = null, t2 = null;
BufferedReader b1, b2;
ServerSocket ss;
Socket s;
PrintWriter p1;

Server() {
    try {
        ss = new ServerSocket(8000);
        System.out.println();
        System.out.println("Server is Waiting . . . . . ");
        s = ss.accept();
        System.out.println();
        System.out.println("Client Connected ! ! ! ");
        t1 = new Thread(this);
        t2 = new Thread(this);
        t1.start();
        t2.start();
    } catch (Exception e) {
        System.out.println(e);
    }
}
//----------------------------------------------------------------------------------------

public void run() {

    if (Thread.currentThread() == t1) {
        try {
            b1 = new BufferedReader(new InputStreamReader(System.in));
            p1 = new PrintWriter(s.getOutputStream(), true);

            do {
                System.out.println();
                messageIN = b1.readLine();
                System.out.println("Server Says : : : "+messageIN);
                p1.println(messageIN);
            } while (!messageIN.equals("END"));
        } catch (Exception ex) {
        }


    } else {
        try {
            b2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
            do {
                messageOUT = b2.readLine();
                System.out.println("Client Says : : : " + messageOUT);
                System.out.println();
            } while (!messageOUT.equals("END"));
        } catch (Exception e) {
        }
    }
}
//----------------------------------------------------------------------------------------

public static void main(String[] args) {
    new Server();
}
}

//-----------------------------------------------------------------------------------------

Client.java

import java.io.*;
import java.net.*;



 public class Client implements Runnable {

String messageIN, messageOUT;
Thread thread1 = null, thread2 = null;
BufferedReader br1, br2;
Socket s;
PrintWriter pw;

Client() {
    try {
        System.out.println();
        System.out.println("Going to connect to Server");
        s = new Socket("localhost", 8000);
        System.out.println();
        System.out.println("Connected");
        thread1 = new Thread(this);
        thread2 = new Thread(this);
        thread1.start();
        thread2.start();
    } catch (Exception ex) {
        System.out.println("ex = " + ex);
    }
    }
   //-----------------------------------------------------------------------------------------

  public void run() {
    if (Thread.currentThread() == thread2) {
        try {
            br1 = new BufferedReader(new InputStreamReader(System.in));

            do {
                System.out.println();                    
                messageIN = br1.readLine();
                System.out.println("Client Says : : "+messageIN);
                pw = new PrintWriter(s.getOutputStream(), true);
                pw.println(messageIN);
            } while (!messageIN.equals("END"));
        } catch (Exception ex) {
        }



    } else {
        try {
            do {
                br2 = new BufferedReader(new InputStreamReader(s.getInputStream()));
                messageOUT = br2.readLine();
                System.out.println("Server Says : : : " + messageOUT);
                System.out.println();
            } while (!messageOUT.equals("END"));

        } catch (Exception e) {
        }

    }
 }

//----------------------------------------------------------------------------------------  
public static void main(String[] args) {
    new Client();
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.