I have a problem that I can't send text to ChatServer. My code:
...
try {
socket.connect(address);
System.out.println("Successfully connected to server!");
Thread fromServerToConsole = new TelnetThread(socket.getInputStream(), System.out);
Thread fromConsoleToServer = new TelnetThread(System.in, socket.getOutputStream());
fromConsoleToServer.setDaemon(true);
fromServerToConsole.start();
fromConsoleToServer.start();
fromServerToConsole.join();
socket.close();
} catch() ...
Where in this try block should I put something like `socket.getOutputStream().write("mystring".bytes())` if I want to send this text "mystring" to the SocketServer after connecting to it?
I tried to put this everywhere and it always didn't work, to send "mystring" I always had to press Enter in console of this client (console is for sending messages from console to the server). And the "mystring" was also printed to client's console, but it has to be printed only on the server's side.
I need to send "mystring" not from console, but automatically after connecting socket to the server.
Please help. Thanks. Sounds like you need to send a line terminator as well.
以上就是Send text to serversocket after connecting to it的详细内容,更多请关注web前端其它相关文章!