码迷,mamicode.com
首页 > 其他好文 > 详细

10_1_网络

时间:2018-05-05 11:16:54      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:output   dos   tput   连接服务器   read   接收   nbsp   net   import   

10_1_网络

1. 客户端往服务器端写数据

TcpClient.java

package com.socket;

 

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

 

public class TcpClient {

 

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

Socket s = new Socket("127.0.0.1", 8888);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF("我是客户端,我要连接服务器,请接收。");

dos.close();

s.close();

}

 

}

 

TcpServer.java

package com.socket;

 

import java.io.DataInputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

 

public class TcpServer {

 

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

ServerSocket ss = new ServerSocket(8888);

while(true) {

Socket s = ss.accept();

DataInputStream dis = new DataInputStream(s.getInputStream());

String ing = dis.readUTF();

System.out.println(ing);

dis.close();

s.close();

}

}

}

 

 

2. 客户端读取服务器数据

TcpServer.java

package com.socket;

 

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

 

public class TcpServer {

 

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

ServerSocket ss = new ServerSocket(8888);

while(true) {

Socket s = ss.accept();

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF("hello client" + "客户端ip为:" +  s.getInetAddress() + "客户端端口为:"+s.getPort());

dos.close();

s.close();

}

}

}

TcpClient.java

package com.socket;

 

import java.io.DataInputStream;

import java.io.IOException;

import java.net.Socket;

import java.net.UnknownHostException;

 

public class TcpClient {

 

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

Socket s = new Socket("127.0.0.1", 8888);

DataInputStream dis = new DataInputStream(s.getInputStream());

System.out.println(dis.readUTF());

dis.close();

s.close();

}

 

}

 

 

3. 客户端跟服务器双向读写

TcpServer.java

package com.socket;

 

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

 

public class TcpServer {

 

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

ServerSocket ss = new ServerSocket(8888);

while(true) {

Socket s = ss.accept();

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

String s1 = null;

if((s1 = dis.readUTF()) != null) {

System.out.println(s1);

}

dos.writeUTF("hello client" + "客户端ip为:" +  s.getInetAddress() + "客户端端口为:"+s.getPort());

dis.close();

dos.close();

s.close();

}

}

}

TcpClient.java

package com.socket;

 

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.Socket;

import java.net.UnknownHostException;

 

public class TcpClient {

 

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

Socket s = new Socket("127.0.0.1", 8888);

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

dos.writeUTF("hello server");

DataInputStream dis = new DataInputStream(s.getInputStream());

String s1 = null;

if((s1 = dis.readUTF()) != null) {

System.out.println(s1);

}

dos.close();

dis.close();

s.close();

}

 

}

 

 

4. 客户端、服务器端双向你说一句,我说一句

TcpServer.java

package com.socket;

 

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

 

public class TcpServer {

 

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

ServerSocket ss = new ServerSocket(8888);

Socket s = ss.accept();

System.out.println("有客户端连接到服务器了。。。。。。。。。。");

BufferedReader ssin = new BufferedReader(new InputStreamReader(s.getInputStream()));

    System.out.println("clinet: " + ssin.readLine());

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

PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

String line = sin.readLine();

while(!line.equals("bye")) {

System.out.println("server: " + line);

out.println(line);

out.flush();

    System.out.println("clinet: " + ssin.readLine());

line = sin.readLine();

}

out.close();

s.close();

sin.close();

ssin.close();

}

}

TcpClient.java

package com.socket;

 

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.Socket;

import java.net.UnknownHostException;

 

import javax.lang.model.type.PrimitiveType;

 

public class TcpClient {

 

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

Socket s = new Socket("127.0.0.1", 8888);

System.out.println("客户端请求连接服务器");

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

PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

BufferedReader ssin = new BufferedReader(new InputStreamReader(s.getInputStream()));

String line = sin.readLine();

while(!line.equals("bye")) {

System.out.println("client:" + line);

out.println(line);

out.flush();

System.out.println("server: " + ssin.readLine());

line = sin.readLine();

}

out.close();

s.close();

sin.close();

ssin.close();

}

}

 

 

5. UDP测试程序

TestUDPServer.java

package Test;

 

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

 

public class TestUDPServer {

 

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

byte[] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b, b.length);

DatagramSocket ds = new DatagramSocket(3456);

while(true) {

ds.receive(dp);

System.out.println(new String(b, 0, dp.getLength()));

}

}

}

TestUDPClient.java

package Test;

 

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetSocketAddress;

 

public class TestUDPClient {

 

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

byte[] b = new String("hello").getBytes();

DatagramPacket dp = new DatagramPacket(b, b.length, new InetSocketAddress("127.0.0.1", 3456));

DatagramSocket ds = new DatagramSocket(8888);

ds.send(dp);

ds.close();

}

}

 

 

6. UDP测试传递Long类型数据

TestUDPServer.java

package Test;

 

import java.io.ByteArrayInputStream;

import java.io.DataInputStream;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

 

public class TestUDPServer {

 

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

byte[] b = new byte[1024];

DatagramPacket dp = new DatagramPacket(b, b.length);

DatagramSocket ds = new DatagramSocket(3456);

while(true) {

ds.receive(dp);

ByteArrayInputStream bais = new ByteArrayInputStream(b);

DataInputStream dis = new DataInputStream(bais);

Long n = dis.readLong();

System.out.println(n);

//System.out.println(new String(b, 0, dp.getLength()));

}

}

}

TestUDPClient.java

package Test;

 

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetSocketAddress;

 

public class TestUDPClient {

 

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

long n = 10000L;

ByteArrayOutputStream bao = new ByteArrayOutputStream();

DataOutputStream dos = new DataOutputStream(bao);

dos.writeLong(n);

byte[] b = bao.toByteArray();

DatagramPacket dp = new DatagramPacket(b, b.length, new InetSocketAddress("127.0.0.1", 3456));

DatagramSocket ds = new DatagramSocket(8888);

ds.send(dp);

ds.close();

}

}

 

10_1_网络

标签:output   dos   tput   连接服务器   read   接收   nbsp   net   import   

原文地址:https://www.cnblogs.com/flyback/p/8993819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!