怎么用JAVA编辑网络监听程序,要源程序!!!
2个回答
展开全部
//前两天刚写的一个程序。应该可以满足楼主的需求
/*提供带有一排红灯的GUI界面,程序运行时监听本机的8888号端口,有客户端连接时,
绿灯亮起,断开则红灯亮起。*/
//server 端的.楼主可以用 命令: "telnet 127.0.0.1 8888"测试或加上client也行
import java.net.*;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Label;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class tcpServer extends Frame {
static Button b = new Button(" ");
static Label l1 = new Label("No connection on port 8888");
public tcpServer() throws HeadlessException {
super("Listening");
setLayout(new FlowLayout(FlowLayout.LEFT));
add(b);
add(l1);
b.setBackground(Color.RED);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String args[]) {
new tcpServer();
int port;
ServerSocket server_socket;
BufferedReader input;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
System.out.println("port = 8888 (default)");
port = 8888;
}
try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port "
+ server_socket.getLocalPort());
// server infinite loop
while (true) {
Socket socket = server_socket.accept();
b.setBackground(Color.GREEN);
l1.setText("Connection"+socket.getInetAddress());
System.out.println("New connection accepted "
+ socket.getInetAddress() + ":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// print received data
try {
while (true) {
String message = input.readLine();
if (message == null)
break;
System.out.println(message);
}
} catch (IOException e) {
System.out.println(e);
}
// connection closed by client
try {
socket.close();
b.setBackground(Color.RED);
l1.setText("No connection on port 8888");
System.out.println("Connection closed by client");
} catch (IOException e) {
System.out.println(e);
}
}
} catch (IOException e) {
System.out.println(e);
}
}
}
//client端的,以下代码可以不用。如想用放到不同文件里编译。再运行
// tcpClient.java by jing5083394
// usage : java tcpClient <server> <port>
// default port is 8888
import java.net.*;
import java.io.*;
public class tcpClient {
public static void main(String[] args) {
int port = 8888;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1;
// read arguments
if(args.length == 2) {
server = args[0];
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1000 (default)");
port = 1500;
}
}
// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}
try {
input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(socket.getOutputStream(),true);
// get user input and transmit it to server
while(true) {
lineToBeSent = input.readLine();
// stop if input line is "."
if(lineToBeSent.equals(".")) break;
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
}
try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
/*提供带有一排红灯的GUI界面,程序运行时监听本机的8888号端口,有客户端连接时,
绿灯亮起,断开则红灯亮起。*/
//server 端的.楼主可以用 命令: "telnet 127.0.0.1 8888"测试或加上client也行
import java.net.*;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Label;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class tcpServer extends Frame {
static Button b = new Button(" ");
static Label l1 = new Label("No connection on port 8888");
public tcpServer() throws HeadlessException {
super("Listening");
setLayout(new FlowLayout(FlowLayout.LEFT));
add(b);
add(l1);
b.setBackground(Color.RED);
setVisible(true);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String args[]) {
new tcpServer();
int port;
ServerSocket server_socket;
BufferedReader input;
try {
port = Integer.parseInt(args[0]);
} catch (Exception e) {
System.out.println("port = 8888 (default)");
port = 8888;
}
try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port "
+ server_socket.getLocalPort());
// server infinite loop
while (true) {
Socket socket = server_socket.accept();
b.setBackground(Color.GREEN);
l1.setText("Connection"+socket.getInetAddress());
System.out.println("New connection accepted "
+ socket.getInetAddress() + ":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// print received data
try {
while (true) {
String message = input.readLine();
if (message == null)
break;
System.out.println(message);
}
} catch (IOException e) {
System.out.println(e);
}
// connection closed by client
try {
socket.close();
b.setBackground(Color.RED);
l1.setText("No connection on port 8888");
System.out.println("Connection closed by client");
} catch (IOException e) {
System.out.println(e);
}
}
} catch (IOException e) {
System.out.println(e);
}
}
}
//client端的,以下代码可以不用。如想用放到不同文件里编译。再运行
// tcpClient.java by jing5083394
// usage : java tcpClient <server> <port>
// default port is 8888
import java.net.*;
import java.io.*;
public class tcpClient {
public static void main(String[] args) {
int port = 8888;
String server = "localhost";
Socket socket = null;
String lineToBeSent;
BufferedReader input;
PrintWriter output;
int ERROR = 1;
// read arguments
if(args.length == 2) {
server = args[0];
try {
port = Integer.parseInt(args[1]);
}
catch (Exception e) {
System.out.println("server port = 1000 (default)");
port = 1500;
}
}
// connect to server
try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}
catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}
try {
input = new BufferedReader(new InputStreamReader(System.in));
output = new PrintWriter(socket.getOutputStream(),true);
// get user input and transmit it to server
while(true) {
lineToBeSent = input.readLine();
// stop if input line is "."
if(lineToBeSent.equals(".")) break;
output.println(lineToBeSent);
}
}
catch (IOException e) {
System.out.println(e);
}
try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
自己写的一段代码,或许对你有点帮助
package org.zcq100.Net;
import java.net.*;
import static org.zcq100.Print.*;
public class UDPTest {
public static void send(){
try{
DatagramSocket ds=new DatagramSocket(6666);
String str="I am s best programner!";
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),6666);
ds.send(dp);
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void Recive(){
try{
byte[] data=new byte[100];
DatagramPacket dp=new DatagramPacket(data,data.length);
DatagramSocket ds=new DatagramSocket(6666);
ds.receive(dp);
ds.close();
println(data);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
if(args.length>0)
Recive();
else
send();
}
}
package org.zcq100.Net;
import java.net.*;
import static org.zcq100.Print.*;
public class UDPTest {
public static void send(){
try{
DatagramSocket ds=new DatagramSocket(6666);
String str="I am s best programner!";
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),6666);
ds.send(dp);
ds.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void Recive(){
try{
byte[] data=new byte[100];
DatagramPacket dp=new DatagramPacket(data,data.length);
DatagramSocket ds=new DatagramSocket(6666);
ds.receive(dp);
ds.close();
println(data);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
if(args.length>0)
Recive();
else
send();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询