[英]Java Sockets. Server-Client communication
I am trying to connect a client with gui with a server withoout gui. 我正在尝试将客户端与gui连接到一个没有gui的服务器。 Connection is being done, but i cant see any messages between these two apps.
正在进行连接,但我无法看到这两个应用之间的任何消息。 (i should get SERVER HERE in client, and CLIENT HERE in server)
(我应该在客户端获取SERVER,在服务器中获取客户端)
Client Connection Code: 客户端连接代码:
@Override
public void ClientRunning(){
try {
connectToServer();
setStreams();
ClientRun();
}catch(EOFException oefException){
showMessage("\n Client terminated the connection\n");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
close();
}
}
public void connectToServer() throws IOException{
showMessage("Attempting Connection... \n");
connection = new Socket(InetAddress.getByName(serverIP),6789);
showMessage("Connected to: "+ connection.getInetAddress().getHostName());
}
public void setStreams() throws IOException{
output = new PrintWriter(connection.getOutputStream(),true);
output.flush();
input= new BufferedReader(new InputStreamReader(connection.getInputStream()));
showMessage("\n Streams are now set. \n");
}
public void close(){
showMessage("\n closing...");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
public void showMessage(final String text){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
cwindow.append(text);
}
});
}
public void sendMessage(String message){
output.write("CLIENT - "+message);
output.flush();
showMessage("\nCLIENT - "+message);
}
private void ClientRun() throws IOException{
String message="CLIENT HERE!";
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
}
(input and output are defined in GUI class in which this Client class is extended to. Defined as "protected BufferedReader input; protected PrintWriter output;") (输入和输出在此类客户端类扩展到的GUI类中定义。定义为“受保护的BufferedReader输入;受保护的PrintWriter输出;”)
Also, servers code: 另外,服务器代码:
public class ServerClass {
private ServerSocket server;
private Socket connection;
private BufferedReader input;
private BufferedWriter output;
public void startServer(){
try{
server=new ServerSocket(6789,100);
while(true){
try{
waitForConnection();
setStreams();
ServerRunning();
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}finally{
close();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect... \n");
connection=server.accept();
showMessage("Now connected to "+ connection.getInetAddress().getHostName());
}
private void setStreams() throws IOException{
output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
output.flush();
input= new BufferedReader(new InputStreamReader(connection.getInputStream()));
showMessage("\n Streams are now set. \n");
}
public void ServerRunning() throws IOException{
String message="SERVER HERE!";
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
}
private void close(){
showMessage("\n Closing connections... \n");
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
private void showMessage(String text){
System.out.println(text);
}
private void sendMessage(String message){
try{
output.write("SERVER - "+message);
output.flush();
showMessage(message);
}catch(IOException ioException){
System.out.println("\n ERROR!");
}
}
Connection seems to be ok, so i don't get whats wrong. 连接似乎没问题,所以我不知道什么是错的。 Any help would be appreciated.
任何帮助,将不胜感激。
PS: i also tried PrintWriter in server as well and also tried a try catch in stream statement, the problem remains. PS:我也在服务器上尝试过PrintWriter,并尝试在流语句中尝试catch,问题依然存在。
You're using readLine()
which expects a newline character. 你正在使用
readLine()
,它需要一个换行符。 either add a \\n
to the messages you are sending or don't use readLine()
. 要么为要发送的消息添加
\\n
,要么不使用readLine()
。
[SOLVED] As TedTrippin and Alfie mentioned readline(), messed my code. [求助]正如TedTrippin和Alfie提到的readline(),搞砸了我的代码。 After adding "\\n" in each message, everything seems to work smooth!
在每条消息中添加“\\ n”后,一切似乎都顺利进行! I also changed printwriter to bufferwritter in client code, and i also added a try-catch function in sendMessage, cause printwritter seems to cause some problem as well for some reason.
我还在客户端代码中将printwriter更改为bufferwritter,并且我还在sendMessage中添加了try-catch函数,因为printwritter似乎也会因某些原因引起一些问题。 Anyway, everything is set and done!
无论如何,一切都已完成! Thanks guys!
多谢你们!
UPDATED CLIENT CODE: 更新的客户代码:
private void ClientRun() throws IOException{
String message="CLIENT HERE! \n"; <----added \n here.
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
}
public void sendMessage(String message){
try{ <-----added try-catch statement
output.write("CLIENT - "+message+"\n"); <----adding "\n" here is super userful, instead of having to add \n in each message.
output.flush();
showMessage(message);
}catch(IOException ioException){
System.out.println("\n ERROR!");
}
}
protected BufferedReader input;
protected BufferedWriter output; <----changed printwriter to BufferWriter
UPDATED SERVER CODE: 更新的服务器代码:
public void ServerRunning() throws IOException{
String message="SERVER HERE! \n"; <--- added \n here as well.
sendMessage(message);
do{
try{
message=input.readLine();
showMessage("\n"+message);
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}
}while(message!="EXIT");
} }
Thanks guys! 多谢你们!
After compairing this code with some I have written in the past using sockets , I noticed you are using output.write(string)
and input.readline()
- These do not mix well, readline()
expects a newline and write
does not give one. 将这段代码与过去使用套接字编写的一些代码进行比较后,我注意到你正在使用
output.write(string)
和input.readline()
- 这些不能很好地混合, readline()
需要一个换行符并且write
不会给出一。
Replace write()
with println()
. 用
println()
替换write()
println()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.