Java编程

java通讯教程

Socket通常也称作“套接字”,用于描述IP地址和端口,可以用来实现不同虚拟机或不同计算机之间的通信。——百度百科

套接字允许应用程序插入到网络中,并与插入到网络中的其他应用程序通信。——《TCP/IP Socket编程·TCP/IP in C》

Socket编程示意图:

服务器需要先启动,等待客户端连接。客户端主动连接服务器,连接成功才能通信。

import java.net.*;import java.io.*;public class Skt字节流S {public static void main(String[] args) {try {ServerSocket sSocket = new ServerSocket(8888);System.out.println(“new ServerSocket”);Socket s = sSocket.accept();System.out.println(“accept()”);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();byte[] b = new byte[1024];int len = is.read(b);String str = new String(b, 0, len);System.out.println(“客户端说:” + str);os.write(“对不起,我们这三碗不过岗”.getBytes());is.close();os.close();s.close();sSocket.close();System.out.println(“服务器端,OVER”);} catch (Exception e) {}}}客户端:

import java.net.*;import java.io.*;public class Skt字节流C {public static void main(String[] args) {try {// Socket s = new Socket(InetAddress.getLocalHost() , 8888);Socket s = new Socket(“127.0.0.1”, 8888);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();os.write(“你好,我要10碗酒,三斤牛肉”.getBytes());byte[] b = new byte[1024];int len = -1;while ((len = is.read(b)) > 0) {String str = new String(b, 0, len);System.out.println(“服务器端的回复:” + str);}is.close();os.close();s.close();System.out.println(“客户端Over”);} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}看效果时,可以一个程序用Eclipse启动,一个用cmd启动,编译时如果有错,可用如下语句:

javac -encoding utf-8 XXX.javacmd运行程序时,不要有包名。

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;public class SktCharacterS {public static void main(String[] args) {try {ServerSocket sSocket = new ServerSocket(8888);Socket s = sSocket.accept();InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader bufR = new BufferedReader(isr);PrintWriter prtW = new PrintWriter(os);// —————————————————–String str = bufR.readLine();System.out.println(“——” + str);prtW.print(“你好,我是服务器”);prtW.flush();bufR.close();isr.close();is.close();prtW.close();os.close();s.close();sSocket.close();} catch (IOException e) {e.printStackTrace();}}}客户端:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;public class SktCharacterC {public static void main(String[] args) {Socket s;try {s = new Socket(InetAddress.getLocalHost(), 8888);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();PrintWriter prtW = new PrintWriter(os);InputStreamReader isr = new InputStreamReader(is);BufferedReader bufR = new BufferedReader(isr);// ————————————————–prtW.println(“你好我是客户机”);prtW.flush();String str = bufR.readLine();System.out.println(“——” + str);bufR.close();isr.close();is.close();prtW.close();os.close();s.close();} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}socket发送文件(基本款)服务端:

import java.net.*;import java.io.*;public class SktFileS {public static void main(String[] args) {try {ServerSocket sSocket = new ServerSocket(8888);Socket s = sSocket.accept();InputStream ips = s.getInputStream();FileOutputStream fos = new FileOutputStream(“recv.png”);byte[] b = new byte[1024];int nLen = -1;while (true) {nLen = ips.read(b);if (nLen == -1) {break;}fos.write(b, 0, nLen);}fos.close();ips.close();s.close();sSocket.close();} catch (Exception e) {} finally {System.out.println(“服务器端,OVER”);}}}客户端:

import java.net.*;import java.io.*;public class SktFileC {public static void main(String[] args) {try {Socket s = new Socket(“127.0.0.1”, 8888);OutputStream ops =

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注