java写服务器端代码 服务器端代码怎么写
编写代码,使用java ServerSocket创建服务器端ServerSocket的过程。
package socket;
创新互联建站-专业网站定制、快速模板网站建设、高性价比鞍山网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式鞍山网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖鞍山地区。费用合理售后完善,十多年实体公司更值得信赖。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketService {
//搭建服务器端
public static void main(String[] args) throws IOException{
SocketService socketService = new SocketService();
//1、a)创建一个服务器端Socket,即SocketService
socketService.oneServer();
}
public void oneServer(){
try{
ServerSocket server=null;
try{
server=new ServerSocket(5209);
//b)指定绑定的端口,并监听此端口。
System.out.println("服务器启动成功");
//创建一个ServerSocket在端口5209监听客户请求
}catch(Exception e) {
System.out.println("没有启动监听:"+e);
//出错,打印出错信息
}
Socket socket=null;
try{
socket=server.accept();
//2、调用accept()方法开始监听,等待客户端的连接
//使用accept()阻塞等待客户请求,有客户
//请求到来则产生一个Socket对象,并继续执行
}catch(Exception e) {
System.out.println("Error."+e);
//出错,打印出错信息
}
//3、获取输入流,并读取客户端信息
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter writer=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
System.out.println("Client:"+in.readLine());
//在标准输出上打印从客户端读入的字符串
line=br.readLine();
//从标准输入读入一字符串
//4、获取输出流,响应客户端的请求
while(!line.equals("end")){
//如果该字符串为 "bye",则停止循环
writer.println(line);
//向客户端输出该字符串
writer.flush();
//刷新输出流,使Client马上收到该字符串
System.out.println("Server:"+line);
//在系统标准输出上打印读入的字符串
System.out.println("Client:"+in.readLine());
//从Client读入一字符串,并打印到标准输出上
line=br.readLine();
//从系统标准输入读入一字符串
} //继续循环
//5、关闭资源
writer.close(); //关闭Socket输出流
in.close(); //关闭Socket输入流
socket.close(); //关闭Socket
server.close(); //关闭ServerSocket
}catch(Exception e) {//出错,打印出错信息
System.out.println("Error."+e);
}
}
}
关于java,soap服务器端的代码怎么写
soapenv:Header/
soapenv:Body
api:getEmp
shopIdstring/shopId
/api:getEmp
/soapenv:Body
/soapenv:Envelope
这个时SOAP协议的标准报文格式,客户端只要发送这样的报文给支持SOAP协议的webservice服务器即可成功调用web service服务
服务端:部署服务只需要自己定义服务的接口和实现类,并用@webservice注解,通过endPoint类直接发布即可
JAVA问题:用socket编写一个服务器端程序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
publicclass Server
{
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server()
{
try
{
ss = new ServerSocket(10000);
while (true)
{
socket = ss.accept();
String RemoteIP = socket.getInetAddress().getHostAddress();
String RemotePort = ":" + socket.getLocalPort();
System.out.println("A clientcome in!IP:" + RemoteIP
+ RemotePort);
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String line = in.readLine();
System.out.println("Cleint sendis :" + line);
out = new PrintWriter(socket.getOutputStream(), true);
out.println("YourMessage Received!");
out.close();
in.close();
socket.close();
}
}
catch (IOException e)
{
out.println("wrong");
}
}
publicstaticvoid main(String[] args)
{
new Server();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
publicclass Client {
Socket socket;
BufferedReader in;
PrintWriter out;
public Client()
{
try
{
System.out.println("Try toConnect to 127.0.0.1:10000");
socket = new Socket("127.0.0.1", 10000);
System.out.println("The ServerConnected!");
System.out.println("Pleaseenter some Character:");
BufferedReader line = new BufferedReader(new
InputStreamReader(System.in));
out = new PrintWriter(socket.getOutputStream(), true);
out.println(line.readLine());
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
System.out.println(in.readLine());
out.close();
in.close();
socket.close();
}
catch (IOException e)
{
out.println("Wrong");
}
}
publicstaticvoid main(String[] args)
{
new Client();
}
}
你再改改代码就可以了.但我没时间帮你调了……
网页标题:java写服务器端代码 服务器端代码怎么写
本文路径:http://ybzwz.com/article/hihogd.html