|
查看: 1348|回复: 12
|
Java Client Server - Socket Programming [已解决]
[复制链接]
|
|
|
嗨,大家好,我要写个普通的client server program,
然后呢,Client需要send message给server,然后server自动的再依不同的 message 做不同的事情也给与Client回复。
初步阶段是,我要确保client 和 server 之间的communication是 client to server 一次, server to client 一次, 连惯性的。
以下是我的 client 和 server 的文件。
现阶段我做到的是 client can keep send message to server。
/*
** test_client.java
*/
import java.io.*;
import java.net.*;
import java.lang.*;
public class test_client
{
public static void main(String args[]) throws Exception
{
DataInputStream input = new DataInputStream(System.in) ;
System.out.print("Enter server address : " ) ;
String str = input.readLine() ;
Socket connection = new Socket( str, 4321 ) ;
PrintWriter out = new PrintWriter(connection.getOutputStream(),true) ;
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())) ;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) ;
System.out.println(connection.getInetAddress()) ;
System.out.println(connection.getOutputStream()) ;
System.out.println(connection.getInputStream()) ;
String sendMsg;
boolean firstWord;
firstWord = true;
while(connection.isConnected() == true)
{
// << PLACE A >>
if(firstWord == true)
{
sendMsg = in.readLine() ;//Statement 1
System.out.println(sendMsg) ;
firstWord = false ;
}
System.out.print(">" ) ;
sendMsg = reader.readLine() ;
out.println(sendMsg) ;
if(sendMsg.equals("EXIT" ))
{
System.out.println("SYSTEM EXIT" ) ;
connection.close() ;
System.exit(1) ;
}
}
}
}
/*
** test_server.java
*/
import java.io.*;
import java.net.*;
import java.lang.*;
public class test_server
{
public static void main(String args[]) throws Exception
{
ServerSocket serverSocket = new ServerSocket(4321) ;
Socket connection = serverSocket.accept() ;
PrintWriter out = new PrintWriter(connection.getOutputStream(),true) ;
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())) ;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)) ;
System.out.println("Establish connection from " + connection.getLocalSocketAddress() + "\n waha" ) ;
String getMsg;
boolean firstWord;
firstWord = true;
while(connection.isConnected() == true)
{
if(firstWord == true)
{
out.println("Welcom to Ublog Server !!!" ) ;
firstWord = false;
}
getMsg = in.readLine();
if(getMsg.isEmpty() == false && getMsg.equals("EXIT" ) == false)
{
if(getMsg.equals("WAZA" ))
{
System.out.println("hey yo" ) ;
out.println("hey yo" ) ;
}
else
{
System.out.println("client_> " + getMsg) ;
}
}
else
{
System.out.println("haa" ) ;
}
if(getMsg.equals("EXIT" ))
{
System.out.println("CLIENT REQUEST EXIT" );
connection.close() ;
System.exit(1) ;
}
}
}
}
问题来了,当我把statement 1 放在 place A 的时候,我的client side收到一次server 的 message 之后,只可以send 3 次 message给server然后就什么都做不到了。一直停留在那个place A。
在client send了两次message给server后,server回复一个message,client又可以在连send 3次message。然后也就什么都做不到了。
我只要server send one time message, client send one time message accordingly.
我相信是我的logic出了问题,可能还有其他的问题吧,请各位帮忙指正. 谢谢 
[ 本帖最后由 nick_khor 于 8-8-2009 10:43 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 6-8-2009 10:45 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-8-2009 10:57 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2009 12:04 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-8-2009 12:30 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-8-2009 01:46 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2009 12:05 PM
|
显示全部楼层
仔细看你就会明白了。
//client
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())) ;
你的 in 是读取 server side 的 message。
然而你看这里
//server
if(getMsg.isEmpty() == false && getMsg.equals("EXIT" ) == false)
{
if(getMsg.equals("WAZA" ))
{
System.out.println("hey yo" ) ;
out.println("hey yo" ) ; //server replied
}
else
{
System.out.println("client_> " + getMsg) ;
//lack of server reply
}
}
因此, 除非你在 client 那里移植输入 WAZA, 你才能够得到server reply, 不然的话, 你的 server 没有reply。 那么
//client
while(connection.isConnected() == true)
{
// << PLACE A >>
sendMsg = in.readLine() ; //Statement 1 // client keep waiting for server reply until time out
if(firstWord == true)
{
System.out.println(sendMsg) ;
firstWord = false ;
}
System.out.print(">" ) ;
sendMsg = reader.readLine() ;
out.println(sendMsg) ;
if(sendMsg.equals("EXIT" ))
{
System.out.println("SYSTEM EXIT" ) ;
connection.close() ;
System.exit(1) ;
}
}
你的 client 就会傻傻的在那里等, 一直到 connection timeout, 然后assign null 去 getMsg. |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-8-2009 07:16 PM
|
显示全部楼层
hm...我摸摸看再回复您
谢咯 onlylonly |
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2009 08:45 PM
|
显示全部楼层
回复 7# onlylonly 的帖子
抱歉写错了, 不是getMsg, 是 sendMsg |
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2009 09:09 PM
|
显示全部楼层
原帖由 nick_khor 于 6-8-2009 10:57 PM 发表 
我的greeting code还有其他目的,而且我是要等 connection establish的时候才send,如果放在LOOP外面的话,connection还没有联接成就就已经send出去了,那client既不是收不到?
我所指的外面是在loop外,印证连接成功后的地方。可以是一个专门等待连接的loop呵。。
再说,我好像没发现你的代码里有 ‘等 connection establish" 的部分 (很明显的缺乏documentation)
头脑灵活一点,逻辑换成想下面那样,不是比较有效率吗?
- while not connected
- Sleep
- Greet connected client
- messageloop until exit
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-8-2009 09:24 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2009 10:17 PM
|
显示全部楼层
回复 10# yeenfei 的帖子
java 里不必如此。 connection 等待无需用户烦恼, 无法连接时自然又 exception。 只要handle exception 就没问题了。
Connection.isConnected() 会自动将 connection handle 好。
但是nick , 你的 coding style 还得多多改进, 不说你的 naming style , 你的 algorithm 也必须改进
1. 将 code modulerize , i.e 分成多个method ( 也就是一般说的 function )
2. comment
3. 别让rudundent looping 浪费了 processor 资源
e.g
while(connection.isConnected() == true)
{
if(firstWord == true)
{
out.println("Welcom to Ublog Server !!!" ) ;
firstWord = false;
}
getMsg = in.readLine();
if(getMsg.isEmpty() == false && getMsg.equals("EXIT" ) == false)
{
if(getMsg.equals("WAZA" ))
{
System.out.println("hey yo" ) ;
out.println("hey yo" ) ;
换成
if(connection.isConnected())
out.println("Welcom to Ublog Server !!!" ) ;
while(connection.isConnected() == true)
{
getMsg = in.readLine();
if(getMsg.isEmpty() == false && getMsg.equals("EXIT" ) == false)
{
if(getMsg.equals("WAZA" ))
{
System.out.println("hey yo" ) ;
out.println("hey yo" ) ;
[ 本帖最后由 onlylonly 于 7-8-2009 10:19 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-8-2009 10:42 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|