import java.net.*;
import java.io.*;
public class CSTest extends Thread
{
private Socket s;
public CSTest(Socket s)
{
this.s=s;
}
public void run()
{
try {
OutputStream os=s.getOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(os);
InputStream is=s.getInputStream();
bos.write("欢迎连接服务器!".getBytes());
byte[] buf=new byte[1024];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
bos.close();
is.close();
s.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)
{
if(args.length>0)
server();
else
client();
}
public static void server()
{
try {
ServerSocket ss=new ServerSocket(6500);
while(true)
{
Socket s = ss.accept();
new CSTest(s).start();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void client()
{
try {
Socket s=new Socket(InetAddress.getByName(null),6500);
OutputStream os=s.getOutputStream();
InputStream is=s.getInputStream();
byte[] buf=new byte[1024];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
os.write("哈哈,狂放不羁想连接服务器".getBytes());
os.close();;
is.close();
s.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
打算做个简单网络聊天工具,今天看了一下.net包,写了个小练手程序,但是为什么不能运行呢?各位帮忙看看。
[此贴子已经被作者于2007-6-27 0:03:00编辑过]