打印本文 打印本文  关闭窗口 关闭窗口  
socket 连接服务器的简单例子
作者:不详  文章来源:不详  点击数  更新时间:2005/4/19 14:54:21  文章录入:ahui  责任编辑:ahui

socket 连接服务器的简单例子
import java.net.Socket;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

public class Main
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("192.168.100.140", 18890);
String loginCmd = "Authyizyizpwd\3";

BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
os.write(loginCmd.getBytes());
os.flush();

BufferedInputStream is = new BufferedInputStream(socket.getInputStream());

StringBuffer sb = new StringBuffer();
while (true)
{
char ch = (char)is.read();
if (ch == '\3') break;
sb.append(ch);
}
System.out.println(sb.toString());

socket.shutdownOutput();
socket.shutdownInput();
socket.close();
}
}

打印本文 打印本文  关闭窗口 关闭窗口