java实现服务端代码 java服务器代码

用java代码实现客户端与服务端建立连接?

套接字 Socket

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了黄州免费建站欢迎大家使用!

import java.net.*;

Server:

ServerSocket server=new ServerSocket(port);//port是端口

Socket socket=server.accept();

//等待客户机的连接请求,若连接,则创建一套接字,并将返回。

Client:

Socket socket=new Socket("host",port);//host主机名(本机:127.0.0.1)

用java多线程实现服务器与客户端之间的文件传输的代码!!!急!!!!

程序分Server和Client

服务器端打开侦听的端口,一有客户端连接就创建两个新的线程来负责这个连接

一个负责客户端发送的信息(ClientMsgCollectThread 类),

另一个负责通过该Socket发送数据(ServerMsgSendThread )

Server.java代码如下:

/*

* 创建日期 2009-3-7

*

* TODO 要更改此生成的文件的模板,请转至

* 窗口 - 首选项 - Java - 代码样式 - 代码模板

*/

package faue.MutiUser;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

/**

* 服务器端

*

* @author Faue

*/

public class Server extends ServerSocket {

private static final int SERVER_PORT = 10000;

/**

* 构造方法,用于实现连接的监听

*

* @throws IOException

*/

public Server() throws IOException {

super(SERVER_PORT);

try {

while (true) {

Socket socket = super.accept();

new Thread(new ClientMsgCollectThread(socket), "getAndShow"

+ socket.getPort()).start();

new Thread(new ServerMsgSendThread(socket), "send"

+ socket.getPort()).start();

}

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws IOException {

new Server();

}

/**

* 该类用于创建接收客户端发来的信息并显示的线程

*

* @author Faue

* @version 1.0.0

*/

class ClientMsgCollectThread implements Runnable {

private Socket client;

private BufferedReader in;

private StringBuffer inputStringBuffer = new StringBuffer("Hello");

/**

* 得到Socket的输入流

*

* @param s

* @throws IOException

*/

public ClientMsgCollectThread(Socket s) throws IOException {

client = s;

in = new BufferedReader(new InputStreamReader(client

.getInputStream(), "GBK"));

}

public void run() {

try {

while (!client.isClosed()) {

inputStringBuffer.delete(0, inputStringBuffer.length());

inputStringBuffer.append(in.readLine());

System.out.println(getMsg(inputStringBuffer.toString()));

}

} catch (IOException e) {

//e.printStackTrace();

System.out.println(client.toString() + " is closed!");

}

}

/**

* 构造显示的字符串

*

* @param line

* @return

*/

private String getMsg(String line) {

return client.toString() + " says:" + line;

}

}

/**

* 该类用于创建发送数据的线程

*

* @author Faue

* @version 1.0.0

*/

class ServerMsgSendThread implements Runnable {

private Socket client;

private PrintWriter out;

private BufferedReader keyboardInput;

private StringBuffer outputStringBuffer = new StringBuffer("Hello");

/**

* 得到键盘的输入流

*

* @param s

* @throws IOException

*/

public ServerMsgSendThread(Socket s) throws IOException {

client = s;

out = new PrintWriter(client.getOutputStream(), true);

keyboardInput = new BufferedReader(new InputStreamReader(System.in));

}

public void run() {

try {

while (!client.isClosed()) {

outputStringBuffer.delete(0, outputStringBuffer.length());

outputStringBuffer.append(keyboardInput.readLine());

out.println(outputStringBuffer.toString());

}

} catch (IOException e) {

//e.printStackTrace();

System.out.println(client.toString() + " is closed!");

}

}

}

}

客户端:

实现基于IP地址的连接,连接后也创建两个线程来实现信息的发送和接收

/*

* 创建日期 2009-3-7

*

*/

package faue.MutiUser;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

/**

* 客户端

*

* @author Faue

*/

public class Client {

private Socket mySocket;

/**

* 创建线程的构造方法

*

* @param IP

* @throws IOException

*/

public Client(String IP) throws IOException {

try {

mySocket = new Socket(IP, 10000);

new Thread(new ServerMsgCollectThread(mySocket), "getAndShow"

+ mySocket.getPort()).start();

new Thread(new ClientMsgSendThread(mySocket), "send"

+ mySocket.getPort()).start();

} catch (IOException e) {

//e.printStackTrace();

System.out.println("Server.IP:" + IP

+ " port:10000 can not be Connected");

}

}

public static void main(String[] args) throws IOException {

try {

new Client(args[0]);

} catch (Exception e) {

System.out.println("输入的IP地址错误");

}

}

/**

* 该类用于创建接收服务端发来的信息并显示的线程

*

* @author Faue

* @version 1.0.0

*/

class ServerMsgCollectThread implements Runnable {

private Socket client;

private BufferedReader in;

private StringBuffer inputStringBuffer = new StringBuffer("Hello");

/**

* 得到Socket的输入流

*

* @param s

* @throws IOException

*/

public ServerMsgCollectThread(Socket s) throws IOException {

client = s;

in = new BufferedReader(new InputStreamReader(client

.getInputStream(), "GBK"));

}

public void run() {

try {

while (!client.isClosed()) {

inputStringBuffer.delete(0, inputStringBuffer.length());

inputStringBuffer.append(in.readLine());

System.out.println(getMsg(inputStringBuffer.toString()));

}

} catch (IOException e) {

//e.printStackTrace();

System.out.println(client.toString() + " is closed!");

System.exit(0);

}

}

/**

* 构造输入字符串

*

* @param line

* @return

*/

private String getMsg(String line) {

return client.toString() + " says:" + line;

}

}

/**

* 该类用于创建发送数据的线程

*

* @author Faue

* @version 1.0.0

*/

class ClientMsgSendThread implements Runnable {

private Socket client;

private PrintWriter out;

private BufferedReader keyboardInput;

private StringBuffer outputStringBuffer = new StringBuffer("Hello");

/**

* 得到键盘的输入流

*

* @param s

* @throws IOException

*/

public ClientMsgSendThread(Socket s) throws IOException {

client = s;

out = new PrintWriter(client.getOutputStream(), true);

keyboardInput = new BufferedReader(new InputStreamReader(System.in));

}

public void run() {

try {

while (!client.isClosed()) {

outputStringBuffer.delete(0, outputStringBuffer.length());

outputStringBuffer.append(keyboardInput.readLine());

out.println(outputStringBuffer.toString());

}

out.println("--- See you, bye! ---");

} catch (IOException e) {

//e.printStackTrace();

System.out.println(client.toString() + " is closed!");

System.exit(0);

}

}

}

}

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!

vaela

关于java,soap服务器端的代码怎么写

soapenv:Header/

soapenv:Body

api:getEmp

shopIdstring/shopId

/api:getEmp

/soapenv:Body

/soapenv:Envelope

这个时SOAP协议的标准报文格式,客户端只要发送这样的报文给支持SOAP协议的webservice服务器即可成功调用web service服务

服务端:部署服务只需要自己定义服务的接口和实现类,并用@webservice注解,通过endPoint类直接发布即可

编写代码,使用java ServerSocket创建服务器端ServerSocket的过程。

package 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;

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实现服务端代码 java服务器代码
网站链接:http://csdahua.cn/article/doecspo.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流