| |

VerySource

 Forgot password?
 Register
Search
View: 709|Reply: 3

socket problem

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-10 07:30:01
| Show all posts |Read mode
public class NetworkActionServer extends Thread {

private static final int serverport = 8899;
Ranch
private Socket serversocket;
DBAction db = new DBAction ();
Ranch
NetworkActionServer (Socket i) {
this.serversocket = i;
}
Ranch
// Get the message from C and return the verification message
public void run () {
try {
Ranch
            // get client mes
            ObjectInputStream InputStream = new ObjectInputStream (serversocket.getInputStream ());
            User usr = (User) InputStream.readObject ();
            // do client mes
            db.insertInto (usr);
            // Return verification message
            ObjectOutputStream OutputStream = new ObjectOutputStream (serversocket.getOutputStream ());
            User [] dbusr = db.selectall ();
            for (int i = 0; i <dbusr.length; i ++) {
                System.out.println (dbusr [i] .getName ());
            OutputStream.writeObject ("a"); /////////////
                   ////////////////////////// Cannot write objects here, socket write error
            //Thread.sleep(3000);
            }
            OutputStream.flush ();
} catch (Exception e) {
e.printStackTrace ();
}
}
Ranch
public static void main (String arg []) {
try {
ServerSocket servercon = new ServerSocket (serverport);
while (true) {
Socket s = servercon.accept ();
Thread t = new NetworkActionServer (s);
t.start ();
}
} catch (Exception e) {
e.printStackTrace ();
}
Ranch
}
}
Just




socket write error ..... everyone help, thank you
Reply

Use magic Report

0

Threads

39

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 Sweden

Post time: 2020-6-1 21:45:01
| Show all posts
Maybe the connection is broken.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-2 19:30:01
| Show all posts
LZ is not so good when naming variables, InputStream and OutputStream are themselves class names
 
 Try to change the variable name first.
 
 The User object implements the serial interface.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-6-14 17:30:02
| Show all posts
The connection is not broken, the User object has been serialized
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list