java中serverSocket是干什么的 怎么用
2个回答
展开全部
serversocket 建立的是socket的服务端,
socket建立的是客户端。
例子
socket和serversocket (2010-05-07 04:17:11)转载▼
public class Server {
public static void main(String[] args) {
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
try {
//创建服务器,并开放3081端口
ServerSocket server=new ServerSocket(3081);
while(true){
//监听服务器端口,一旦有数据发送过来,那么就将数据封装成socket对象
//如果没有数据发送过来,那么这时处于线程阻塞状态,不会向下继续执行
socket=server.accept();
System.out.println("客户端信息:"+socket.getRemoteSocketAddress());
//从socket中得到读取流,该流中有客户端发送过来的数据
InputStream in=socket.getInputStream();
//InputStreamReader将字节流转化为字符流
br=new BufferedReader(new InputStreamReader(in));
//行读取客户端数据
String info=br.readLine();
System.out.println(info);
OutputStream out=socket.getOutputStream();
pw=new PrintWriter(out);
pw.println("服务器说:我扁死你");
pw.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pw.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Client {
public static void main(String[] args) {
Socket socket=null;
PrintWriter pw=null;
BufferedReader br=null;
try {
//创建socket对象,并指明服务器的IP地址和端口号
socket=new Socket("localhost",3081);
//得到socket发送数据的输出流
OutputStream out=socket.getOutputStream();
//将字节流包装成字符流
pw=new PrintWriter(out);
//向服务器发送数据
pw.println("客户端说:建军闷烧");
//刷新流,确保数据能写到服务器
pw.flush();
InputStream in=socket.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
String info=br.readLine();
System.out.println(info);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Client {
public static void main(String[] args) {
Socket socket=null;
PrintWriter pw=null;
BufferedReader br=null;
try {
//创建socket对象,并指明服务器的IP地址和端口号
socket=new Socket("localhost",3081);
//得到socket发送数据的输出流
OutputStream out=socket.getOutputStream();
//将字节流包装成字符流
pw=new PrintWriter(out);
//向服务器发送数据
pw.println("客户端说:建军闷烧");
//刷新流,确保数据能写到服务器
pw.flush();
InputStream in=socket.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
String info=br.readLine();
System.out.println(info);
} catch (Exception e) {
// TODO
socket建立的是客户端。
例子
socket和serversocket (2010-05-07 04:17:11)转载▼
public class Server {
public static void main(String[] args) {
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
try {
//创建服务器,并开放3081端口
ServerSocket server=new ServerSocket(3081);
while(true){
//监听服务器端口,一旦有数据发送过来,那么就将数据封装成socket对象
//如果没有数据发送过来,那么这时处于线程阻塞状态,不会向下继续执行
socket=server.accept();
System.out.println("客户端信息:"+socket.getRemoteSocketAddress());
//从socket中得到读取流,该流中有客户端发送过来的数据
InputStream in=socket.getInputStream();
//InputStreamReader将字节流转化为字符流
br=new BufferedReader(new InputStreamReader(in));
//行读取客户端数据
String info=br.readLine();
System.out.println(info);
OutputStream out=socket.getOutputStream();
pw=new PrintWriter(out);
pw.println("服务器说:我扁死你");
pw.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
pw.close();
br.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Client {
public static void main(String[] args) {
Socket socket=null;
PrintWriter pw=null;
BufferedReader br=null;
try {
//创建socket对象,并指明服务器的IP地址和端口号
socket=new Socket("localhost",3081);
//得到socket发送数据的输出流
OutputStream out=socket.getOutputStream();
//将字节流包装成字符流
pw=new PrintWriter(out);
//向服务器发送数据
pw.println("客户端说:建军闷烧");
//刷新流,确保数据能写到服务器
pw.flush();
InputStream in=socket.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
String info=br.readLine();
System.out.println(info);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
pw.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Client {
public static void main(String[] args) {
Socket socket=null;
PrintWriter pw=null;
BufferedReader br=null;
try {
//创建socket对象,并指明服务器的IP地址和端口号
socket=new Socket("localhost",3081);
//得到socket发送数据的输出流
OutputStream out=socket.getOutputStream();
//将字节流包装成字符流
pw=new PrintWriter(out);
//向服务器发送数据
pw.println("客户端说:建军闷烧");
//刷新流,确保数据能写到服务器
pw.flush();
InputStream in=socket.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
String info=br.readLine();
System.out.println(info);
} catch (Exception e) {
// TODO
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询