java中socket与serversocket的问题

2024-12-04 04:07:38
推荐回答(3个)
回答1:

给你个我们学习的时候书上的一个小例子...楼主仔细研究一下...

就是实现在两台计算机在局域网的条件下聊天...DOS界面下..

当然也可以在eclipse控制台中啦..

1.
SimpleServer.java

package netpack;
import java.net.*;
public class SimpleServer
{
private static final int PORT_NUMBER=10000;
public static void main(String[] args)
{
ServerSocket server=null;
Socket theSocket=null;
try
{
server=new ServerSocket(PORT_NUMBER);
SimpleServerWorker worker;
Thread thread;
while(true)
{
theSocket=server.accept();
worker=new SimpleServerWorker(theSocket);
thread=new Thread(worker);
thread.start();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
server.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}

2.
SimpleClient.java

package netpack;
import java.net.*;
import java.io.*;

public class SimpleClient
{
private static final int PORT_NUMBER=10000;
public static void main(String[] args)
{
Socket theSocket=null;
BufferedReader theReader=null;
PrintWriter theWriter=null;
String theAddress=null;
/*if(args.length!=1)
{
System.out.println("Usage: java SimpleClient

");
System.exit(1);
}
else
theAddress=args[0];
*/
try
{
theSocket=new Socket(theAddress,PORT_NUMBER);
theReader=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
theWriter=new PrintWriter(new OutputStreamWriter(theSocket.getOutputStream()));
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String mesg;
while(true)
{
//发送信息到服务器端
System.out.print("Enter your message ('end' to exit): ");
mesg=stdin.readLine();
theWriter.println(mesg);
theWriter.flush();

//结束连接
if(mesg.equalsIgnoreCase("end"))
break;

//从服务器端接受信息
mesg=theReader.readLine();
System.out.println(">> "+mesg);
}
}
catch(IOException e)
{
System.out.println("Exception: "+e.getMessage());
}
finally
{
try
{
theWriter.close();
theReader.close();
theSocket.close();
}
catch(Exception e)
{}
}
}
}

3.SimpleServerWorker.java

package netpack;
import java.io.*;
import java.net.*;

public class SimpleServerWorker implements Runnable
{
private Socket theSocket=null;
private BufferedReader theReader=null;
private PrintWriter theWriter=null;
public SimpleServerWorker(Socket theSocket)
{
this.theSocket=theSocket;
}
//服务器端的执行入口
public void run()
{
try
{
InetAddress inet=theSocket.getInetAddress();
String clientName=inet.getHostName();

System.out.println(clientName+" is connected");
theReader=new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
theWriter=new PrintWriter(new OutputStreamWriter(theSocket.getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String mesg;
while(true)
{
//从客户端接受信息
mesg=theReader.readLine();
System.out.println(">>"+mesg);
//如果客户信息为"end",则结束
if(mesg.equalsIgnoreCase("end"))
break;
mesg=br.readLine();
//把客户端传递的信息返回
theWriter.println(mesg);
theWriter.flush();
}
System.out.println(clientName+" is disconnected");

}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
theWriter.close();
theReader.close();
theSocket.close();
}
catch(Exception e)
{}
}
}
}

楼主可以回去试试看...

仔细看一下就知道其实不是很难..

也不难理解...

祝楼主早日成功!

回答2:

Socket soc = new Socket();
soc.connect(new InetAddress("-----"));

根据要连接的服务器地址,建立连接;

ServerSocket ssoc = new ServerSocket();
ssoc.accept();此方法接受连接,返回一个Socket对象,表示客护端的请求连接;

客户端和服务器端的 socket 对象都可以调用getInputStream();或者getOutputStream();
来获得输出输入流。

回答3:

不要问怎么去做
你自己先做一下,没弄明白,把代码发上来再问,这样对你会有很大帮助的,软件是做出来的,不是想出来的