谁能给个异步短连接的socket通信的客户端代码,发送到邮箱[email protected],感激不尽,确认后给分,谢谢
解决方案 »
- 求struts2玩的顺的人!
- crystalReport2008 web for java----passResultSet issue
- MySql查询问题,请教高手是否能这的做!
- 怎么在ComboBox的下拉列表中加入DhtmlXTree树形控件
- struts中在jsp中的表单提交问题困惑
- 初学java web,用access做数据库的部署问题?
- JSP如何实现像这样的时间日期组件?【见图】
- 请教关于SQL语句!!(急)
- to 大哥大姐大虾:
- 使用apache+tomcat实现泛域名解析,RewriteRule规则写法
- jsp2.0中application的范畴是指什么?
- Applet存储文件的问题
MSN:[email protected]
邮箱:[email protected]
import java.io.*;public class myclient
{
public static void main(String args[])
{
Socket socket;
String s = "";
InputStream Is;
OutputStream Os;
BufferedReader DIS;
PrintStream out;
try
{
//向主机名为args[0]的服务器申请连接
//注意端口号要与服务器保持一致:4321
socket = new Socket(InetAddress.getLocalHost(),4000);
System.out.println("client ok");
System.out.println("************************************************");
System.out.println("");
//获得对应socket的输入/输出流
Is = socket.getInputStream();
Os = socket.getOutputStream();
//建立数据流
DIS = new BufferedReader(new InputStreamReader(Is));
out = new PrintStream(Os);
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
while (true)
{
System.out.print("你说:");
s = in.readLine(); //读取用户输入的字符串
out.println(s); //将读取得字符串传给server
if (s.trim().equals("BYE"))
break; //如果是"BYE",就退出
else
{
System.out.println("");
System.out.println("请等待服务端消息...");
System.out.println("");
}
s = DIS.readLine(); //从服务器获得字符串
System.out.println("服务器说:" + s); //打印字符串
if (s.trim().equals("BYE"))
break; //如果是"BYE",就退出
}
//关闭连接
DIS.close(); //关闭数据输入流
out.close(); //关闭数据输出流
Is.close(); //关闭输入流
Os.close(); //关闭输出流
socket.close(); //关闭socket
}
catch (Exception e)
{
System.out.println("Error:" + e);
}
}
}