java简单客户端服务端
2013-06-07 22:29
239 查看
import java.net.*;
import java.io.*;
public class Service
{
public static void main(String[] args)
{
InputStream
in = null;
OutputStream out = null;
int port
= 33333;
try {
ServerSocket ss = new ServerSocket(port);
while(true) {
Socket s = ss.accept();
System.out.println("a is connected!");
in = s.getInputStream();
out = s.getOutputStream();
DataInputStream
din = new DataInputStream(in);
DataOutputStream
dout = new DataOutputStream(out);
dout.writeUTF("Hello Client");
String s1 = null;
if ((s1=din.readUTF()) != null) {
System.out.println(s1);
System.out.println("ip address is " + s.getInetAddress());
System.out.println("the port is " + s.getPort());
}
dout.flush();
dout.close();
din.close();
s.close();
}
} catch (IOException e) {
System.out.println("the server socket is error!");
}
}
}
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args)
{
InputStream
in = null;
OutputStream out = null;
int port = 33333;
try {
Socket s = new Socket("localhost", port);
in = s.getInputStream();
out = s.getOutputStream();
DataInputStream din = new DataInputStream(in);
String s1 = null;
if ((s1=din.readUTF()) != null) {
System.out.println(s1);
}
DataOutputStream dout = new DataOutputStream(out);
dout.writeUTF("Hello Server !!!");
din.close();
dout.close();
s.close();
} catch (ConnectException e) {
System.out.println("the socket is connect exception !");
} catch (IOException e) {
System.out.println("the io exception is in socket!");
}
}
}
import java.io.*;
public class Service
{
public static void main(String[] args)
{
InputStream
in = null;
OutputStream out = null;
int port
= 33333;
try {
ServerSocket ss = new ServerSocket(port);
while(true) {
Socket s = ss.accept();
System.out.println("a is connected!");
in = s.getInputStream();
out = s.getOutputStream();
DataInputStream
din = new DataInputStream(in);
DataOutputStream
dout = new DataOutputStream(out);
dout.writeUTF("Hello Client");
String s1 = null;
if ((s1=din.readUTF()) != null) {
System.out.println(s1);
System.out.println("ip address is " + s.getInetAddress());
System.out.println("the port is " + s.getPort());
}
dout.flush();
dout.close();
din.close();
s.close();
}
} catch (IOException e) {
System.out.println("the server socket is error!");
}
}
}
import java.net.*;
import java.io.*;
public class Client
{
public static void main(String[] args)
{
InputStream
in = null;
OutputStream out = null;
int port = 33333;
try {
Socket s = new Socket("localhost", port);
in = s.getInputStream();
out = s.getOutputStream();
DataInputStream din = new DataInputStream(in);
String s1 = null;
if ((s1=din.readUTF()) != null) {
System.out.println(s1);
}
DataOutputStream dout = new DataOutputStream(out);
dout.writeUTF("Hello Server !!!");
din.close();
dout.close();
s.close();
} catch (ConnectException e) {
System.out.println("the socket is connect exception !");
} catch (IOException e) {
System.out.println("the io exception is in socket!");
}
}
}
相关文章推荐
- java中自己实现一个服务端对应多个客户端的简单代码
- C# client 与java netty 服务端的简单通信,客户端采用Unity。
- Java网络编程之简单的服务端客户端应用实例
- Java中利用socket实现简单的服务端与客户端的通信(入门级)
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- Java中利用socket实现简单的服务端与客户端的通信(基础级)
- C# client 与java netty 服务端的简单通信,客户端采用Unity。
- Java网络编程(一)- 一个简单的服务端/客户端应用程序
- JAVA 服务端和客户端Socket通信的简单例子
- 简单java Web Service服务端和Android客户端
- Java网络编程(一)- 一个简单的服务端/客户端应用程序
- Java简单实现TCP服务端和客户端通信
- 文件上传简单服务端和客户端 Java 实现
- socket ( java ) 简单多个客户端、服务端通信(多线程)
- Java中利用socket实现简单的服务端与客户端的通信(中级)——实现任意双向通信
- [Java]命令行模拟TCP客户端与服务端的简单小程序遇到的问题(基础不牢!扎实和亲手实践比什么都重要!)
- android客户端 与javaweb服务端的简单理解
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- JAVA NIO实现服务端与客户端简单数据传输 JAVA NIO 之一
- 简单的java socket客户端和服务端示例