一、客户端发送数据
package com.xiafan.study02;
import java.net.*;
import java.io.*;
public class TestTcpClient {
public static void main(String[] args) {
InetAddress serverip = null;
int port;
Socket socket = null;
OutputStream output = null;
try {
serverip = InetAddress.getByName("127.0.0.1");
port = 9999;
socket = new Socket(serverip,port);
output = socket.getOutputStream();
output.write("helloworld".getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (output!=null)
{
try {
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(socket!=null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
二、服务端接收数据
package com.xiafan.study02;
import java.net.*;
import java.io.*;
public class TestTcpServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket1 = null;
InputStream input = null;
ByteArrayOutputStream byteoutput = null;
try {
serverSocket = new ServerSocket(9999);
socket1 = serverSocket.accept();
input = socket1.getInputStream();
byteoutput = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len=input.read(buffer))!=-1)
{
byteoutput.write(buffer,0,len);
}
System.out.println(byteoutput.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if(byteoutput!=null)
{
try {
byteoutput.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if(input!=null)
{
try {
input.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (socket1!=null)
{
try {
socket1.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (serverSocket!=null)
{
try {
serverSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
三、客户端发送文件
package com.xiafan.study02;
import java.io.*;
import java.net.*;
public class TestFileClient {
public static void main(String[] args) throws Exception{
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("shark.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer))!=-1)
{
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is2 = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = is2.read(buffer2))!=-1)
{
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
fis.close();
os.close();
socket.close();
}
}
四、服务端接受文件
package com.xiafan.study02;
import java.io.*;
import java.net.*;
public class TestFileServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9090);
Socket socket1 = serverSocket.accept();
InputStream is = socket1.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("copy.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1)
{
fos.write(buffer,0,len);
}
OutputStream output = socket1.getOutputStream();
output.write("已接受信息".getBytes());
fos.close();
is.close();
socket1.close();
serverSocket.close();
}
}