| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 499 人关注过本帖
标题:是一个盛传的网络监听的程序,有错误!赐教!
只看楼主 加入收藏
as333com
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-1-31
收藏
 问题点数:0 回复次数:2 
是一个盛传的网络监听的程序,有错误!赐教!
// tcpServer.java by fpont 3/2000

// usage : java tcpServer <port number>.
// default port is 1500.
// connection to be closed by client.
// this server handles only 1 connection.

import java.net.*;
import java.io.*;

public class tcpServer {

public static void main(String args[]) {

int port;
ServerSocket server_socket;
BufferedReader input;
try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
System.out.println("port = 1500 (default)");
port = 1500;
}

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();
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();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}





// tcpClient.java by fpont 3/2000

// usage : java tcpClient <server> <port>
// default port is 1500

import java.net.*;
import java.io.*;

public class tcpClient {



public static void main(String[] args) {

int port = 1500;
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);
}
}
}
搜索更多相关主题的帖子: 监听 盛传 网络 
2006-04-05 21:42
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
哪里有错,我这里运行的很好。

可惜不是你,陪我到最后
2006-04-06 11:00
as333com
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-1-31
收藏
得分:0 
xiexie !!!
2006-04-07 19:49
快速回复:是一个盛传的网络监听的程序,有错误!赐教!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023694 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved