TCP连接
发送文字
package com. harbin. lesson02;
import java. io. IOException;
import java. io. OutputStream;
import java. net. InetAddress;
import java. net. Socket;
import java. util. Scanner;
public class TcpClientDemo01 {
public static void main ( String[ ] args) {
OutputStream outputStream = null;
Socket socket = null;
while ( true ) {
try {
InetAddress serverIp = InetAddress. getByName ( "127.0.0.1" ) ;
int port = 9999 ;
socket = new Socket ( serverIp, port) ;
outputStream = socket. getOutputStream ( ) ;
Scanner scanner = new Scanner ( System. in) ;
if ( scanner. hasNext ( ) ) {
String s = scanner. nextLine ( ) ;
outputStream. write ( s. getBytes ( ) ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( outputStream != null) {
try {
outputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( socket != null) {
try {
socket. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
}
package com. harbin. lesson02;
import java. io. ByteArrayOutputStream;
import java. io. IOException;
import java. io. InputStream;
import java. net. ServerSocket;
import java. net. Socket;
public class TcpServerDemo01 {
public static void main ( String[ ] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
serverSocket = new ServerSocket ( 9999 ) ;
while ( true ) {
socket = serverSocket. accept ( ) ;
inputStream = socket. getInputStream ( ) ;
byteArrayOutputStream = new ByteArrayOutputStream ( ) ;
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len= inputStream. read ( buffer) ) != - 1 ) {
byteArrayOutputStream. write ( buffer, 0 , len) ;
}
System. out. println ( byteArrayOutputStream. toString ( ) ) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( byteArrayOutputStream!= null) {
try {
byteArrayOutputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( inputStream!= null) {
try {
inputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( socket != null) {
try {
socket. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( serverSocket != null) {
try {
serverSocket. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
发送文件
package com. harbin. lesson02;
import java. io. ByteArrayOutputStream;
import java. io. FileInputStream;
import java. io. InputStream;
import java. io. OutputStream;
import java. net. InetAddress;
import java. net. Socket;
public class TcpClientDemo02 {
public static void main ( String[ ] args) {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
InetAddress inetAddress = InetAddress. getByName ( "127.0.0.1" ) ;
int port = 9000 ;
socket = new Socket ( inetAddress, port) ;
outputStream = socket. getOutputStream ( ) ;
fileInputStream = new FileInputStream ( "xuexiao.jpg" ) ;
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len= fileInputStream. read ( buffer) ) != - 1 ) {
outputStream. write ( buffer, 0 , len) ;
}
socket. shutdownOutput ( ) ;
inputStream = socket. getInputStream ( ) ;
byteArrayOutputStream = new ByteArrayOutputStream ( ) ;
byte [ ] buff = new byte [ 1024 ] ;
int lens;
while ( ( lens= inputStream. read ( buff) ) != - 1 ) {
byteArrayOutputStream. write ( buff, 0 , lens) ;
}
System. out. println ( byteArrayOutputStream. toString ( ) ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
} finally {
try {
byteArrayOutputStream. close ( ) ;
inputStream. close ( ) ;
fileInputStream. close ( ) ;
outputStream. close ( ) ;
socket. close ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
}
package com. harbin. lesson02;
import java. io. *;
import java. net. ServerSocket;
import java. net. Socket;
public class TcpServerDemo02 {
public static void main ( String[ ] args) {
ServerSocket serverSocket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
Socket socket = null;
try {
serverSocket = new ServerSocket ( 9000 ) ;
socket = serverSocket. accept ( ) ;
inputStream = socket. getInputStream ( ) ;
fileOutputStream = new FileOutputStream ( new File ( "receive.jpg" ) ) ;
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len= inputStream. read ( buffer) ) != - 1 ) {
fileOutputStream. write ( buffer, 0 , len) ;
}
OutputStream outputStream = socket. getOutputStream ( ) ;
outputStream. write ( "服务端接收完毕,可以断开" . getBytes ( ) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
try {
fileOutputStream. close ( ) ;
inputStream. close ( ) ;
socket. close ( ) ;
serverSocket. close ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
}
UDP连接
一次监听
package com. harbin. lesson03;
import java. net. DatagramPacket;
import java. net. DatagramSocket;
import java. net. InetAddress;
public class UdpClientDemo01 {
public static void main ( String[ ] args) throws Exception{
DatagramSocket datagramSocket = new DatagramSocket ( ) ;
String msg = "hello,udpServer" ;
InetAddress name = InetAddress. getByName ( "127.0.0.1" ) ;
int port = 9090 ;
DatagramPacket datagramPacket = new DatagramPacket ( msg. getBytes ( ) , 0 , msg. getBytes ( ) . length, name, port) ;
datagramSocket. send ( datagramPacket) ;
datagramSocket. close ( ) ;
}
}
package com. harbin. lesson03;
import java. net. DatagramPacket;
import java. net. DatagramSocket;
public class UdpServerDemo01 {
public static void main ( String[ ] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket ( 9090 ) ;
byte [ ] buffer = new byte [ 1024 ] ;
DatagramPacket datagramPacket = new DatagramPacket ( buffer, 0 , buffer. length) ;
datagramSocket. receive ( datagramPacket) ;
System. out. println ( datagramPacket. getAddress ( ) ) ;
System. out. println ( new String ( datagramPacket. getData ( ) , 0 , datagramPacket. getLength ( ) ) ) ;
datagramSocket. close ( ) ;
}
}
持续监听
package com. harbin. chat;
import java. io. BufferedReader;
import java. io. InputStreamReader;
import java. net. DatagramPacket;
import java. net. DatagramSocket;
import java. net. InetSocketAddress;
public class UdpSenderDemo01 {
public static void main ( String[ ] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket ( 8888 ) ;
BufferedReader reader = new BufferedReader ( new InputStreamReader ( System. in) ) ;
while ( true ) {
String data = reader. readLine ( ) ;
byte [ ] dataBytes = data. getBytes ( ) ;
DatagramPacket datagramPacket = new DatagramPacket ( dataBytes, 0 , dataBytes. length, new InetSocketAddress ( "localhost" , 6666 ) ) ;
datagramSocket. send ( datagramPacket) ;
if ( data. equals ( "bye" ) ) {
break ;
}
}
datagramSocket. close ( ) ;
}
}
package com. harbin. chat;
import java. net. DatagramPacket;
import java. net. DatagramSocket;
public class UdpReceiveDemo01 {
public static void main ( String[ ] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket ( 6666 ) ;
while ( true ) {
byte [ ] container = new byte [ 1024 ] ;
DatagramPacket datagramPacket = new DatagramPacket ( container, 0 , container. length) ;
datagramSocket. receive ( datagramPacket) ;
byte [ ] data = datagramPacket. getData ( ) ;
String receiveData = new String ( data, 0 , data. length) ;
System. out. println ( receiveData) ;
if ( receiveData. equals ( "bye" ) ) {
break ;
}
}
datagramSocket. close ( ) ;
}
}