java 网络编程基础入门

——–java网络:

1.public class Socket

extends Object;// Socket客户端

This class implements client sockets (also called just “sockets”). A socket is an endpoint for communication between two machines.
The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.
// 构造:

  1. Socket(String host, int port)

Creates a stream socket and connects it to the specified port number on the named host.

  1. Socket(InetAddress address, int port)

Creates a stream socket and connects it to the specified port number at the specified IP address.         
// 方法:

1.public void close()

throws IOException;

//  ServerSocket

public class ServerSocket

extends Object;// Socket服务器端

This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that , and then possibly returns a result to the requester.
构造:

public ServerSocket(int port)

throws IOException

Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port.
方法:

public Socket accept()

throws IOException

Listens for a connection to be made to this socket and accepts it. The method blocks(阻塞) until a connection is made.

public void close()

throws IOException

Closes this socket. Any thread currently blocked in accept() will throw a SocketException.
———-实现Socket客户端:

package com.corejava;

import java.net.Socket;

import java.io.*;

import java.util.Scanner;

public class SocketClient

{

public static void main(String[] args) throws Exception

{

Socket s = new Socket(“localhost”, 8189); // 创建指定主机、指定端口的链接对象

InputStream input = s.getInputStream(); // 获取链接输入流,读取发送的响应信息

OutputStream output = s.getOutputStream(); // 获取链接输出流,向发送请求

Scanner in = new Scanner(input); // 读取的文本

PrintStream out = new PrintStream(output, true);// 向发送文本请求信息

Scanner usein = new Scanner(System.in); // 接收客户端用户的输入

usein.useDelimiter(“\n”); // 回车为换行符

if (s != null)

{

System.out.println(“找到指定,创建连接成功!”);

}

while (in.hasNextLine()) // 服务器响应信息

{

System.out.println(in.nextLine()); // 打印服务器的响应信息

String line = null;

if (usein.hasNextLine()) // 提示用户标准输入

{

line = usein.nextLine(); // 接收用户标准输入

out.println(line);

}

if (line.trim().equals(“bye”)) // 用户要求断开链接

{

System.out.println(“服务器已关闭当前链接!”);

s.close();

}

}

}

}

/*

ubuntu@xu-desktop:~$ java com.corejava.SocketClient

找到指定服务器,创建连接成功!

你好,连接服务器成功,输入[bye]退出当前连接

hello!

Echo:hello!

我是客户端!

Echo:我是客户端!

这是一个socket演示

Echo:这是一个socket演示

再见

Echo:再见

bye

服务器已关闭当前链接!

*/

// 此类C/S的需要维护服务器端和客户端两部分代码:比如QQ服务器和客户端软件;

// 而B/S架构只需要维护服务器端,客户端只是一个浏览器!

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java 网络编程基础入门
本文地址:https://www.zhiletu.com/archives-168.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微