当前位置: 代码迷 >> Java相关 >> [求助]关于.net包的问题。
  详细解决方案

[求助]关于.net包的问题。

热度:131   发布时间:2007-06-27 00:01:13.0
[求助]关于.net包的问题。

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编辑过]

搜索更多相关的解决方案: private  Socket  public  服务器  import  

----------------解决方案--------------------------------------------------------

这是因为你这句:

程序代码:

byte[] buf=new byte[1024];
int len=is.read(buf);


会导致阻赛,因为is一直都读不到1024字节的数据.
下面是我稍稍修改过的代码,可以从客服端输入信息,服务器将返回相同的信息,一直到客服端Say:"bye"
程序代码:

import java.net.*;
import java.io.*;
import java.util.*;
public class CSTest extends Thread
{

private Socket s;
public CSTest(Socket s)
{
this.s=s;
}
public void run()
{
try {
OutputStream os=s.getOutputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));
os.write(\"欢迎连接服务器!\n\".getBytes());
String line;
while(true){
line =br.readLine();
System.out.println(\"[\"+s+\"] say:\"+line);
os.write((line+\"\n\").getBytes());
if(line.equals(\"bye\")){
System.out.println(\"[\"+s+\"] disconnected!\");
break;
}
}
os.close();
br.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(6600);
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),6600);
OutputStream os=s.getOutputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());
String line;
Scanner scan =new Scanner(System.in);
while(true){
System.out.print(\">:\");
os.write((scan.nextLine()+\"\n\").getBytes());
line =br.readLine();
System.out.print(\"<:\"+line+\"\n\");
if(line.equals(\"bye\")) break;
}
os.close();;
br.close();
s.close();
}
catch (Exception ex) {
ex.printStackTrace();
}

}
}


----------------解决方案--------------------------------------------------------

哦 哈哈,谢谢Eastsun老哥。。我看看。


----------------解决方案--------------------------------------------------------
  相关解决方案