3个回答
展开全部
以下是一个展示java使用tcp通讯的简单例子,包括服务器和客户端代码:
/**
*TCPServer
*/
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
/**
*TCPClient
*/
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
/**
*TCPServer
*/
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
/**
*TCPClient
*/
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
展开全部
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleClient {
public static void main(String[] args) throws Exception {
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8888);//连接某台机器(127.0.0.1)上的某个端口(8888)
PrintWriter pw=new PrintWriter(socket.getOutputStream());//得到Socket连接的输出流
pw.println("你好服务器!!");//向服务器发送数据(有可能在缓存里面不会立刻发送,所以要下面的语句)
pw.flush();//立刻发送数据
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//取得Socket连接的输入流
System.out.println(in.readLine());//打印取得的数据
Thread.sleep(1000);
}
}
这是一个简单的客户端连接发送例子
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class SimpleClient {
public static void main(String[] args) throws Exception {
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8888);//连接某台机器(127.0.0.1)上的某个端口(8888)
PrintWriter pw=new PrintWriter(socket.getOutputStream());//得到Socket连接的输出流
pw.println("你好服务器!!");//向服务器发送数据(有可能在缓存里面不会立刻发送,所以要下面的语句)
pw.flush();//立刻发送数据
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//取得Socket连接的输入流
System.out.println(in.readLine());//打印取得的数据
Thread.sleep(1000);
}
}
这是一个简单的客户端连接发送例子
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Socket 类和SocketServer类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询