java中socket与serversocket的问题
答案:3 悬赏:40 手机版
解决时间 2021-02-16 05:51
- 提问者网友:鼻尖触碰
- 2021-02-15 23:29
java中socket与serversocket的问题
最佳答案
- 五星知识达人网友:慢性怪人
- 2021-02-15 23:58
给你个我们学习的时候书上的一个小例子...楼主仔细研究一下...
就是实现在两台计算机在局域网的条件下聊天...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;
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)
{}
}
}
}
楼主可以回去试试看...
仔细看一下就知道其实不是很难..
也不难理解...
祝楼主早日成功!
就是实现在两台计算机在局域网的条件下聊天...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;
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)
{}
}
}
}
楼主可以回去试试看...
仔细看一下就知道其实不是很难..
也不难理解...
祝楼主早日成功!
全部回答
- 1楼网友:动情书生
- 2021-02-16 01:36
不要问怎么去做
你自己先做一下,没弄明白,把代码发上来再问,这样对你会有很大帮助的,软件是做出来的,不是想出来的
你自己先做一下,没弄明白,把代码发上来再问,这样对你会有很大帮助的,软件是做出来的,不是想出来的
- 2楼网友:你哪知我潦倒为你
- 2021-02-16 01:03
Socket soc = new Socket();
soc.connect(new InetAddress("-----"));
根据要连接的服务器地址,建立连接;
ServerSocket ssoc = new ServerSocket();
ssoc.accept();此方法接受连接,返回一个Socket对象,表示客护端的请求连接;
客户端和服务器端的 socket 对象都可以调用getInputStream();或者getOutputStream();
来获得输出输入流。
soc.connect(new InetAddress("-----"));
根据要连接的服务器地址,建立连接;
ServerSocket ssoc = new ServerSocket();
ssoc.accept();此方法接受连接,返回一个Socket对象,表示客护端的请求连接;
客户端和服务器端的 socket 对象都可以调用getInputStream();或者getOutputStream();
来获得输出输入流。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯