I am new to writing servers and clients so I'm not to sure what I am doing here.
I have the server and the client working with the client connecting to the server and sending the message back and forth between the two like it should but my problem is after it sends the message once the client disconnects. I was able to fix it by lopping the Socket but I figured making it reconnect after each time is really inefficient so thats why I am here.
Heres my code;
import java.net.*; import java.io.*; public class Client { static DataInputStream in; static Socket socket; static DataOutputStream out; static String ip = "localHost"; static int port = 5555; static String sent; static String recived; static BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); public static void main(String[] args) throws IOException, UnknownHostException { System.out.println("Connecting to server "+ip+" on port "+port); socket = new Socket(ip, port); while (true) { out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); System.out.println("Type your Message:"); System.out.flush(); sent = stdin.readLine(); out.writeUTF(sent); recived = in.readUTF(); System.out.println(recived); } } }
Heres what I was doing:
public void... { while (true) { System.out.println("Connecting to server "+ip+" on port "+port); socket = new Socket(ip, port); ... } }
So basically it was reconnecting every time it preformed a function
I'm wanting to know how to make it connect and stay connected until disconnected by the server (I can do the disconnecting part myself).