西西软件园多重安全检测下载网站、值得信赖的软件下载站!
西西首页 常用软件 软件下载 安卓软件 游戏下载 安卓游戏 MAC应用 驱动下载 安卓电视
系统工具网络工具媒体工具图形图像聊天工具应用软件编程开发手机软件安卓应用电脑安全字体素材

java socket服务端

  • java socket服务端
  • 软件大小:2KB
  • 更新时间:2013-08-16 10:46
  • 软件语言:中文
  • 软件厂商:
  • 软件类别:国产软件 / 免费软件 / 编程辅助
  • 软件等级:2级
  • 应用平台:WinAll, WinXP
  • 官方网站:暂无
好评:50%
坏评:50%

装机必备软件

软件介绍

一对一的服务端和客户机:一个服务端只能同时服务一个客户端

服务端:

import java.io.*;
import java.net.*;

public class MyServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678);
Socket client=server.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
if(str.equals("end"))
break;
}
client.close();
}
}

accept()方法说明:
public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
A new Socket s is created and, if there is a security manager, the security manager's checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.

客户端

import java.net.*;
import java.io.*;

public class Client{
static Socket server;

public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
//标准输入
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));

while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}

一对多的服务端和客户机:一个服务端只能同时服务多个客户端,需要使用多线程来实现

服务端:调用具体的处理线程完成处理
import java.io.*;
import java.net.*;

public class MyMultiServer {
public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678);
while (true){
Socket client=server.accept();
ChildTh child=new ChildTh(client);//用socket实例初始化具体的处理线程对象
//使用Runnable接口和使用extends Thread的区别:
// 如果是使用Runnable接口,需要这么写:
new Thread(child).start();
//注意:不能写成:child.run();
// 如果使用extends Thread,只需要child.start()就可以,因为start()是Thread的方法
}
}
}

处理线程类:
import java.io.*;
import java.net.*;
public class ChildTh implements Runnable{
private Socket client;
public ChildTh(Socket client){
this.client=client;
}

public void run(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
while(true){
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
if(str.equals("end"))
break;
}

client.close();
}catch(Exception e){
System.out.println("error in the close the socket!");
e.printStackTrace();
}
finally{

}
}

}

客户端:
import java.net.*;
import java.io.*;

public class Client{
static Socket server;

public static void main(String[] args)throws Exception{
server=new Socket(InetAddress.getLocalHost(),5678);
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
PrintWriter out=new PrintWriter(server.getOutputStream());
//标准输入
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));

while(true){
String str=wt.readLine();
out.println(str);
out.flush();
if(str.equals("end")){
break;
}
System.out.println(in.readLine());
}
server.close();
}
}
软件标签: socket

软件截图

java socket服务端

    其他版本下载

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)

    下载帮助下载帮助西西破解版软件均来自互联网, 如有侵犯您的版权, 请与我们联系。

    TOP
    软件下载