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

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

以前写的,照贴了。。。服务器端:import java.awt.*\x0d\x0aimport java.awt.event.WindowAdapter\x0d\x0aimport java.awt.event.WindowEvent\x0d\x0aimport java.io.*\x0d\x0aimport java.net.*/*6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。\x0d\x0a* 客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。\x0d\x0a* (20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)*/\x0d\x0apublic class N4BT6 extends Frame\x0d\x0a{\x0d\x0aDatagramSocket socket \x0d\x0aDatagramPacket packet byte[] buf \x0d\x0aFile file \x0d\x0aFileInputStream input\x0d\x0aString message = "该文件不存在"\x0d\x0aTextArea text\x0d\x0apublic N4BT6(String title)\x0d\x0a{\x0d\x0asuper(title)\x0d\x0atext = new TextArea(6,4)\x0d\x0aadd(text)\x0d\x0asetSize(400, 300)\x0d\x0asetVisible(true)\x0d\x0aaddWindowListener(new WindowAdapter()\x0d\x0a{\x0d\x0apublic void windowClosing(WindowEvent e)\x0d\x0a{\x0d\x0adispose()\x0d\x0a}\x0d\x0a})\x0d\x0a\x0d\x0abuf = new byte[1024]\x0d\x0atry\x0d\x0a{\x0d\x0asocket = new DatagramSocket(1230)\x0d\x0apacket = new DatagramPacket(buf, buf.length)\x0d\x0asocket.receive(packet)\x0d\x0afile = new File(new String(packet.getData()))\x0d\x0asocket = new DatagramSocket()\x0d\x0a} \x0d\x0acatch (Exception e)\x0d\x0a{e.printStackTrace()\x0d\x0a}\x0d\x0a\x0d\x0aif(file.exists())\x0d\x0a{\x0d\x0atry\x0d\x0a{\x0d\x0abuf = new byte[(int)file.length()]\x0d\x0apacket = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),1234)\x0d\x0ainput = new FileInputStream(file)\x0d\x0ainput.read(buf)\x0d\x0asocket.send(packet)\x0d\x0a}\x0d\x0acatch (IOException e) \x0d\x0a{\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0aelse\x0d\x0a{\x0d\x0atry\x0d\x0a{\x0d\x0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,\x0d\x0aInetAddress.getLocalHost(),1234)\x0d\x0asocket.send(packet)\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0apublic static void main(String[] args)\x0d\x0a{\x0d\x0anew N4BT6("Server")\x0d\x0a}\x0d\x0a}\x0d\x0a客户端:import java.awt.*\x0d\x0aimport java.awt.event.*\x0d\x0aimport java.net.DatagramPacket\x0d\x0aimport java.net.DatagramSocket\x0d\x0aimport java.net.InetAddresspublic class N4BT6_2 extends Frame\x0d\x0a{\x0d\x0aTextArea text\x0d\x0aString message = "Q.txt"\x0d\x0aDatagramSocket socket \x0d\x0aDatagramPacket packet\x0d\x0abyte[] buf\x0d\x0apublic N4BT6_2(String title)\x0d\x0a{\x0d\x0asuper(title)\x0d\x0atext = new TextArea(6,4)\x0d\x0aadd(text)\x0d\x0asetSize(400, 300)\x0d\x0asetVisible(true)\x0d\x0aaddWindowListener(new WindowAdapter()\x0d\x0a{\x0d\x0apublic void windowClosing(WindowEvent e)\x0d\x0a{\x0d\x0adispose()\x0d\x0a}\x0d\x0a})\x0d\x0atry\x0d\x0a{\x0d\x0a\x0d\x0asocket = new DatagramSocket()\x0d\x0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,\x0d\x0aInetAddress.getLocalHost(),1230)\x0d\x0asocket.send(packet)\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a\x0d\x0atry\x0d\x0a{\x0d\x0abuf = new byte[1024]\x0d\x0asocket = new DatagramSocket(1234)\x0d\x0apacket = new DatagramPacket(buf,buf.length)\x0d\x0asocket.receive(packet)\x0d\x0atext.append(new String(buf))\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace()\x0d\x0a}\x0d\x0a}\x0d\x0apublic static void main(String[] args)\x0d\x0a{\x0d\x0anew N4BT6_2("Client")\x0d\x0a}\x0d\x0a}

<soapenv:Header/>

<soapenv:Body>

<api:getEmp>

<shopId>string</shopId>

</api:getEmp>

</soapenv:Body>

</soapenv:Envelope>

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

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

服务器监听端口 做个无限循环 接到一个连接就创建一个通道线程,并将通道线程存储到一个list集合中

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStreamReader

import java.io.PrintWriter

import java.net.ServerSocket

import java.net.Socket

import java.text.SimpleDateFormat

import java.util.ArrayList

import java.util.Date

import java.util.List

/*

* 4.用socket通讯写出多个客户端和一个服务器端的通讯,

* 要求客户发送数据后能够回显相同的数据(回显功能)(实用TCP方式)。

*/

public class Test4Server {

// 主入口

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

scoketServer()

}

// 开启的tcp8888监听端口

public static void scoketServer() throws IOException {

ServerSocket server = new ServerSocket(8888)

while (true) {

// 未连通前线程阻塞,连通后开启一个socket通道线程后继续监听8888端口

Socket socket = server.accept()

System.out.println(socket.getInetAddress().getHostAddress()

+ "连接进入")

new SocketThread(socket).start()

}

}

}

// 一个服务器端口中监听多个客服端通道线程

class SocketThread extends Thread {

// 所有通道写入流的集合

private static List<PrintWriter>list =new ArrayList<PrintWriter>()

private BufferedReader bufferedReader

private PrintWriter printWriter

public SocketThread(Socket socket) throws IOException {

this.bufferedReader = new BufferedReader(new InputStreamReader(socket

.getInputStream()))

this.printWriter = new PrintWriter(socket.getOutputStream())

list.add(printWriter)

}

@Override

public void run() {

String string = null

while (true) {

try {

// 服务器在通道中读到的信息回显给客服端

string = bufferedReader.readLine()

System.out.println("客服端信息:" + string)

for(PrintWriter printWriter:list ){

printWriter.write("服务器回显:" + string + "\r\n")

printWriter.flush()

}

} catch (IOException e) {

}

}

}

}

客服端代码 可以用在局域网中用多台来连接测试

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStreamReader

import java.io.PrintWriter

import java.net.Socket

import java.util.Scanner

public class Test4Client {

public static Object obj = new Object()

// 客服端的主入口

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

socketClient()

}

// 与服务器连通地址本机(127.0.0.1),局域网中其他机器是(服务器在局域网中的ip地址) 端口都是8888

public static void socketClient() throws IOException {

Socket socket = new Socket("127.0.0.1", 8888)

if (socket.isConnected()) {

// 如果连接成功了就开启写和读的进程

new writer(socket).start()

new read(socket).start()

} else {

System.out.println("服务器未开启")

}

}

}

// 写入到通道的线程

class writer extends Thread {

@SuppressWarnings("unused")

private Socket socket

private PrintWriter printWriter

private Scanner scanner = new Scanner(System.in)

private String str = null

public writer(Socket socket) throws IOException {

this.socket = socket

this.printWriter = new PrintWriter(socket.getOutputStream())

}

@Override

public void run() {

scanner.useDelimiter("\r\n")

while (true) {

System.out.print("请输入信息:")

// 产生扫描器的线程阻塞

str = scanner.next()

System.out.println("我说:"+str)

printWriter.write(str + "\r\n")

printWriter.flush()

try {

Thread.sleep(200)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}

// 从通道中读取的线程

class read extends Thread {

private Socket socket

private BufferedReader bufferedReader

private String str = null

public read(Socket socket) throws IOException {

this.socket = socket

this.bufferedReader = new BufferedReader(new InputStreamReader(socket

.getInputStream()))

}

@Override

public void run() {

while (true) {

try {

str = bufferedReader.readLine()

System.out.println(str)

} catch (IOException e) {

}

try {

Thread.sleep(200)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

}

}


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/260519.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-16
下一篇2023-04-16

发表评论

登录后才能评论

评论列表(0条)

    保存