我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java服务器通信代码 java服务间通信

JAVA socket通信

public class Client {

10多年的淮阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整淮阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“淮阳网站设计”,“淮阳网站推广”以来,每个客户项目都认真落实执行。

public static void main(String[] args) {

String s=null;

Socket mysocket;

DataInputStream in=null;

DataOutputStream out=null;

try {

mysocket=new Socket("127.0.0.1",4331);

in=new DataInputStream(mysocket.getInputStream());

out=new DataOutputStream(mysocket.getOutputStream());

for(int k=1;k100;k=k+2){

out.writeUTF(""+k);

s=in.readUTF();

System.out.println("客户收到"+s);

Thread.sleep(500);

}

} catch (Exception e) {

System.out.println("服务器已断开"+e);

}

}

}

public class Server {

public static void main(String[] args) {

ServerSocket server=null;

Socket you=null;

String s=null;

DataOutputStream out=null;

DataInputStream in=null;

try {

server=new ServerSocket(4331);

} catch (Exception e) {

System.out.println(e);

}

try {

System.out.println("等待客户呼叫");

you=server.accept();

out=new DataOutputStream(you.getOutputStream());

in=new DataInputStream(you.getInputStream());

while(true){

s=in.readUTF();

int m=Integer.parseInt(s);

out.writeUTF("你好,我是服务器");

out.writeUTF("你说的数乘2后是:"+2*m);

System.out.println("服务器收到:"+s);

Thread.sleep(500);

}

} catch (Exception e) {

System.out.println("客户端已断开"+e);

}

}

}

很简单的服务器客户端程序

java网络编程应该怎样在客户端和服务器间实现通信?

以前写的,照贴了。。。服务器端:import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.*;

import java.net.*;/*6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。

* 客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。

* (20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)*/

public class N4BT6 extends Frame

{

DatagramSocket socket ;

DatagramPacket packet ;byte[] buf ;

File file ;

FileInputStream input;

String message = "该文件不存在";

TextArea text;

public N4BT6(String title)

{

super(title);

text = new TextArea(6,4);

add(text);

setSize(400, 300);

setVisible(true);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dispose();

}

});

buf = new byte[1024];

try

{

socket = new DatagramSocket(1230);

packet = new DatagramPacket(buf, buf.length);

socket.receive(packet);

file = new File(new String(packet.getData()));

socket = new DatagramSocket();

}

catch (Exception e)

{e.printStackTrace();

}

if(file.exists())

{

try

{

buf = new byte[(int)file.length()];

packet = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),1234);

input = new FileInputStream(file);

input.read(buf);

socket.send(packet);

}

catch (IOException e)

{

e.printStackTrace();

}

}

else

{

try

{

packet = new DatagramPacket(message.getBytes(),message.getBytes().length,

InetAddress.getLocalHost(),1234);

socket.send(packet);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

public static void main(String[] args)

{

new N4BT6("Server");

}

}

客户端:import java.awt.*;

import java.awt.event.*;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;public class N4BT6_2 extends Frame

{

TextArea text;

String message = "Q.txt";

DatagramSocket socket ;

DatagramPacket packet;

byte[] buf;

public N4BT6_2(String title)

{

super(title);

text = new TextArea(6,4);

add(text);

setSize(400, 300);

setVisible(true);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dispose();

}

});

try

{

socket = new DatagramSocket();

packet = new DatagramPacket(message.getBytes(),message.getBytes().length,

InetAddress.getLocalHost(),1230);

socket.send(packet);

}

catch (Exception e)

{

e.printStackTrace();

}

try

{

buf = new byte[1024];

socket = new DatagramSocket(1234);

packet = new DatagramPacket(buf,buf.length);

socket.receive(packet);

text.append(new String(buf));

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

new N4BT6_2("Client");

}

}

用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服务器通信代码 java服务间通信
本文来源:http://mswzjz.cn/article/dogppeh.html

其他资讯