以下是一个展示java使用tcp通讯的简单例子,包括服务器和客户端代码:
创新互联建站长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为无为企业提供专业的网站制作、网站建设,无为网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
/**
*TCPServer
*/
import java.io.*;
import java点虐 .*;
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点虐 .*;
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.awt.event.*;
import javax.swing.*;
import java.io.*;
import java点虐 .InetAddress;
import java点虐 .Socket;
public class ChatTCPSocketJFrame extends JFrame implements ActionListener
{
private String name; //网名
private Socket socket; //TCP Socket对象
private JTextArea text_receiver; //显示对话内容的文本区
private JTextField text_sender; //输入发送内容的文本行
private JButton button_send,button_leave; //发送和离线按钮
private PrintWriter cout; //字符输出流对象
public ChatTCPSocketJFrame(String name, Socket socket) throws IOException//提供图形用户界面
{
super("聊天室 "+name+" "+InetAddress.getLocalHost()+" : "+socket.getLocalPort());
this.setBounds(320,240,400,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver = new JTextArea();
this.text_receiver.setEditable(false); //文本区不可编辑
this.getContentPane().add(new JScrollPane(this.text_receiver));
JPanel panel = new JPanel();
this.getContentPane().add(panel,"South");
this.text_sender = new JTextField(16);
panel.add(this.text_sender);
button_send = new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
button_leave = new JButton("离线");
panel.add(button_leave);
button_leave.addActionListener(this);
this.setVisible(true);
this.name = name;
this.socket = socket;
this.cout = new PrintWriter(socket.getOutputStream(),true);//获得Socket对象的输出流,立即flush
this.cout.println(name); //发送自己网名给对方
BufferedReader cin=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获得Socket对象的输入流,创建字符输入流
String aline=cin.readLine(); //接收到对方网名
text_receiver.append("连接"+aline+"成功"+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
while (aline!=null !aline.equals("bye")) //从输入流接收对方发来的字符串
{ //"bye"是约定的离线信息
text_receiver.append(aline+"\r\n"); //显示对方发来的内容
aline= cin.readLine();
}
cin.close();
cout.close();
socket.close(); //关闭Socket连接
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
public ChatTCPSocketJFrame(String name, String host, int port) throws IOException //客户端
{
this(name, new Socket(host, port)); //客户端向指定主机的端口发出TCP连接请求
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button_send) //发送按钮
{
this.cout.println(name+" 说:"+text_sender.getText()); //通过流发送给对方
text_receiver.append("我说:"+text_sender.getText()+"\n");
text_sender.setText("");
}
if (e.getSource()==button_leave) //离线
{
text_receiver.append("我离线\n");
this.cout.println(name+"离线\n"+"bye"); //发送给对方离线约定
button_send.setEnabled(false);
button_leave.setEnabled(false);
}
}
public static void main(String args[]) throws IOException
{
new ChatTCPSocketJFrame("小蜜蜂", "127.0.0.1", 2001);
}
}
//服务端程序
import java点虐 .*;
import java.io.*;
public class ChatTCPServer
{
public ChatTCPServer(int port, String name) throws IOException //约定端口、网名
{ //本机IP地址和指定端口构成服务端的Socket
ServerSocket server = new ServerSocket(port); //ServerSocket提供TCP连接服务
Socket client = server.accept(); //等待接收客户端的连接申请
new ChatTCPSocketJFrame(name,client); //服务端的聊天室,图形用户界面同客户端
server.close();
}
public static void main(String args[]) throws IOException
{
new ChatTCPServer(2001, "花仙子"); //启动服务端,约定端口,指定网名
}
}
import java点虐 .ServerSocket;
import java点虐 .Socket;
public class TcpServer
{
public static void main(String[] args) throws Exception
{
// 创建服务器端的socket对象
ServerSocket ss = new ServerSocket(5000);
// 监听连接
Socket socket = ss.accept();
// 直到连接建立好之后代码才会往下执行
System.out.println("Connected Successfully!");
}
}
import java点虐 .Socket;
public class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("127.0.0.1", 5000);
}
}