简体   繁体   中英

Why this socket data sent only once to the Java server?

I just want to a programme network game and which needs communication between Client Start... Server Yes... Client Give me cards... (Server don't respond on this code and if i have to create always new socket how i will get same address.... localhost is just for test purpose) Socket running only once I can't understand why????????

i am listeening on server by while true but it gets only one string and don't get any thing on next attempt....

server is just listening and reading

package client;     

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

public class Client {                        

    Client() {                         
        try {
        Socket socket = new Socket("localhost",2222);
        int i=0;
        while(i<100){
            i++;
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os,true);
            pw.println(i);
        }                          
        } catch(IOException e){
            System.out.println("Client Constructor Exception");
        }
    }

    public static void main(String[] args) throws Exception{
        Client client = new Client();
    }
}

Code of Server is..... pleasse help me

package server;

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

public class Server {

    Server() {
        try{
            serverSocket = new ServerSocket(2222);
        } catch(IOException e) {
            System.out.println("IOException in Server Constructor");
        }
    }

    public void operate() {
        try{
 //           Control control = new Control();
            while (true) {
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
               System.out.println(br.readLine());
                //control.recieve(socket, br.readLine());
            }
        } catch(IOException e) {
            System.out.println("IOException in Server operate function");
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public static void main(String[] args) throws IOException {
        Server server = new Server();
        server.operate();
    }

    private ServerSocket serverSocket;
    private Socket socket;
}

Output is:

run: Start

why it does not run 100 times.....

You are creating a new socket every time inside the loop, you want to get

socket = new Socket("localhost",2222); 

outside of the infinite loop

In server code you have to move server.accept() outside the loop. Try this one. It would work. Along with this i tried with instant reply from server.

server code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket serverSocket;
    private Socket socket;

    Server() {
        try {
            serverSocket = new ServerSocket(2222);
        } catch (IOException e) {
            System.out.println("IOException in Server Constructor");
        }
    }

    public void operate() {
        try {
            // Control control = new Control();
            socket = serverSocket.accept();
            while (true) {
                System.out.println("Server starts listening on "
                        + serverSocket.getLocalPort());

                System.out.println("Server accepts connection.");

                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os, true);
                pw.println("Hi Client");

//              Thread.sleep(1000);
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                System.out.println(br.readLine());
                System.out.println("reply sent to client");
                // control.recieve(socket, br.readLine());
            }
        } catch (IOException /*| InterruptedException*/ e) {
            System.out.println("IOException in Server operate function");
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public static void main(String[] args) throws IOException {
        Server server = new Server();
        server.operate();
    }

}

Client code:

package com.java.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    Client() {
        try {
            Socket socket;
             socket = new Socket("localhost",2222);

            // InputStream in = socket.getInputStream();
            while (true) {
//              socket = new Socket("localhost", 2222);
                System.out.println("Socket - " + socket.getLocalPort());
                OutputStream os = socket.getOutputStream();
                InputStream is = socket.getInputStream();
                PrintWriter pw = new PrintWriter(os, true);
                pw.println("Hi Server");
//              Thread.sleep(2000);
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                System.out.println("Waiting for server reply..");
                System.out.println(br.readLine());
                System.out.println("Got reply from server..");
                // pw.flush();
            }
        } catch (IOException/* | /*InterruptedException*/ e) {
            e.printStackTrace();
            System.out.println("Client Constructor Exception");
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client();
    }

}
Socket socket = new Socket("localhost",2222);  
OutputStream os=null;   
PrintWriter pw=null;     

while(true){                                   
    os = socket.getOutputStream();                   
    pw = new PrintWriter(os,true);                     
    pw.println("Start");                       
}          
Socket socket = new Socket("localhost",2222);  
OutputStream os=null;   
PrintWriter pw=null;     

while(true){                                   
  os = socket.getOutputStream();                   
  pw = new PrintWriter(os,true);                     
  pw.println("Start");  
  pw.flush();  // to empty the writer buffer                     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM