学习.net包,想给自己留下点什么,有点简单,但是却是最基础的部分
客户端:TestTCPClient.java
服务端TestTCPServet.java
客户端:TestTCPClient.java
package
net;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestTCPClient
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1",6767);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Hello I am bulktree");
dos.flush();
dos.close();
s.close();
}
catch(UnknownHostException ue)
{
ue.printStackTrace();
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
}
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestTCPClient
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1",6767);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Hello I am bulktree");
dos.flush();
dos.close();
s.close();
}
catch(UnknownHostException ue)
{
ue.printStackTrace();
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
}
服务端TestTCPServet.java
package
net;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTCPServer
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(6767);
Socket s = ss.accept();
System.out.println("A Client Connection!");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTCPServer
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(6767);
Socket s = ss.accept();
System.out.println("A Client Connection!");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}