客户端
- 链接服务器Socket
- 发送消息
package com.ayv.try02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcoClientDemo01 {
public static void main(String[] args) {
Socket socket =null;
OutputStream os = null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port=9999;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
os.write("hello world".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务的端口 serverSocket
- 等待用户的链接 accept
- 接收用户的消息
package com.ayv.try02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServeDemo02 {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket =null;
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept();
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.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();
}
}
}
}
}
review:查询IP和端口
package com.ayv.try01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetadderss {
public static void main(String[] args) {
try {
InetAddress intAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(intAddress1);
InetAddress intAddress3 = InetAddress.getByName("localhost");
System.out.println(intAddress3);
InetAddress intAddress4 = InetAddress.getLocalHost();
System.out.println(intAddress4);
InetAddress intAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(intAddress2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
发送文件
package com.ayv.try02;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDamo2 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
package com.ayv.try02;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientDamo2 {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("观望.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
fis.close();
os.close();
socket.close();
}
}