//服务端
public class TcpServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(6666); Socket socket = ss.accept();
//客户端接收得到消息
// OutputStream os = socket.getOutputStream();
// os.write("welcome".getBytes()); InputStream is = socket.getInputStream(); byte[] buffer = new byte[1024]; int length; while (-1 != (length = is.read(buffer, 0, buffer.length)))
{
String str = new String(buffer, 0, length); System.out.println(str);
}
//客户端接收不到消息
OutputStream os = socket.getOutputStream();
os.write("welcome".getBytes()); is.close();
os.close();
socket.close();
}
}
//客户端public class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost", 6666); InputStream is = socket.getInputStream();
// 服务端接收的到消息
// OutputStream os = socket.getOutputStream();
// os.write("hello".getBytes()); byte[] buffer = new byte[1024]; int length; while (-1 != (length = is.read(buffer, 0, buffer.length)))
{
String str = new String(buffer, 0, length); System.out.println(str);
}
// 服务端接收不到消息
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes()); is.close();
os.close();
socket.close(); }
}
public class TcpServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(6666); Socket socket = ss.accept();
//客户端接收得到消息
// OutputStream os = socket.getOutputStream();
// os.write("welcome".getBytes()); InputStream is = socket.getInputStream(); byte[] buffer = new byte[1024]; int length; while (-1 != (length = is.read(buffer, 0, buffer.length)))
{
String str = new String(buffer, 0, length); System.out.println(str);
}
//客户端接收不到消息
OutputStream os = socket.getOutputStream();
os.write("welcome".getBytes()); is.close();
os.close();
socket.close();
}
}
//客户端public class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("localhost", 6666); InputStream is = socket.getInputStream();
// 服务端接收的到消息
// OutputStream os = socket.getOutputStream();
// os.write("hello".getBytes()); byte[] buffer = new byte[1024]; int length; while (-1 != (length = is.read(buffer, 0, buffer.length)))
{
String str = new String(buffer, 0, length); System.out.println(str);
}
// 服务端接收不到消息
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes()); is.close();
os.close();
socket.close(); }
}
public class Server {
public static void main(String[] args) throws IOException { int port = Integer.valueOf(args[0]);
new ServerThread(port).start();
} private static class ServerThread extends Thread {
ServerSocket serverSocket; public ServerThread(int port) {
try {
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(10000);
} catch (IOException e) {
}
} @Override
public void run() {
while (true) {
try {
Socket server = serverSocket.accept();
DataInputStream dis = new DataInputStream(server.getInputStream());
System.out.println("服务器收到客户端的消息:" + dis.readUTF());
DataOutputStream dos = new DataOutputStream(server.getOutputStream());
dos.writeUTF("服务器:");
server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
}
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException { String add = args[0];
int port = Integer.valueOf(args[1]);
String message = args[2]; Socket socket = new Socket(add, port);
DataOutputStream dps = new DataOutputStream(socket.getOutputStream());
dps.writeUTF("客户端:" + message);
DataInputStream dis = new DataInputStream(socket.getInputStream());
dis.readUTF();
socket.close(); }}
// 服务端接收不到消息
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
放在循环的上面
client和server交互,是客户端向服务器端推数据。
服务器端响应数据返回到客户端http://blog.csdn.net/x2570799116/article/details/77570642
http://blog.csdn.net/x2570799116/article/details/77583595
推荐两篇博文可以看一下