这是源程序
import java.io.*;
import java.net.*;public class SocketTest
{ public static void main(String[] args)
{ try
{ Socket s = new Socket("www.sina.com.cn",
8080); BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
boolean more = true;
while (more)
{ String line = in.readLine();
if (line == null) more = false;
else
System.out.println(line);
} }
catch (IOException e)
{ System.out.println("Error" + e);
}
}
}
import java.io.*;
import java.net.*;public class SocketTest
{ public static void main(String[] args)
{ try
{ Socket s = new Socket("www.sina.com.cn",
8080); BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
boolean more = true;
while (more)
{ String line = in.readLine();
if (line == null) more = false;
else
System.out.println(line);
} }
catch (IOException e)
{ System.out.println("Error" + e);
}
}
}
当然被拒,sina不是你家的:)
老大,你写人家sina的地址,你又没有这个权限,把这儿改成你自己的IP吧,写127.0.0.1也行啊
你找个ftp服务器试试吧
所有的socket编程中,ftp最简单。对照cuteftp上面的代码,你很容易写对那些交互式的命令。
telnet最难,给你个程序吧,反正刚好我打开了。
package guitest;import java.io.*;
import java.net.*;
import java.awt.*;
import java.io.*;
import java.lang.*;public class auto_telnet {
public static void main(String args[]){
TelnetClient tn = new TelnetClient();
tn.connect("172.16.3.254",23);
//tn.disconnect();
}} // TelnetClient
// An AWT component that makes a telnet connection to a given host and port
class TelnetClient implements Runnable
{
boolean debug = false;
String host; // remote host
int port; // remote port
Socket s; // connection to remote host
InputStream in; // data from other end
OutputStream out; // data to other end
Thread th; // thread for reading data
Font fn; // current font
Image back; // backing image
int x, y; // cursor position (in chars)
int chw,chh; // size of a char (in pixels)
int chd; // offset of char from baseline
int width,height; // size of applet (in pixels)
int w,h; // size of applet (in chars)
char ch[][]; // array of characters on display String term = "dumb"; // what this terminal claims to be
boolean echo; // echo keys sent? String instring = ""; // input string
String outstring = ""; // output string boolean logined; // indecated if logined
boolean lsed; // indecated if 'ls' command had commited String username = ""; // valueable username
String ipaddress = ""; // ip address
String check_time = ""; // check time // connect
// Connect to the given host and port
void connect(String givenhost, int givenport)
{
host = givenhost; port = givenport;
// reset display
echo = true; // Open connection
try {
try {
if ((s = new Socket(host,port)) == null) {
display("Failed to connect to host "+host+"\n");
return;
}
}
catch(UnknownHostException e) {
display("Host " + host + " not found\n");
return;
}
}
catch(IOException e) {
display("Failed to connect to host "+host+"\n");
return;
}
try {
in = s.getInputStream();
out = s.getOutputStream();
}
catch(IOException e) {
if (debug) System.out.println("Failed to get stream from socket");
System.exit(5);
}
display("Connected to "+host+"\n");
if (debug) System.out.println("Connected to host"); // Begin thread for reading data
th = new Thread(this);
th.start();
} // disconnect
// Close the connection
void disconnect()
{
System.out.println(instring);
System.out.println(outstring);
if (th != null) {
display("\nDisconnected from "+host+"\n");
th.interrupt();
th = null;
s = null; in = null; out = null;
}
} // clearch
// Clear the backing array of characters and reset the cursor
// run
// Loop forever, reading and displaying characters
public void run()
{
while(true) displaych(readch()); } // readch
// Read one char from other end, or die
char readch()
{
System.out.println("readch()");
int c = 0;
try{
c = in.read();
instring = instring + (char)c;
// whenever "login" prompted if((instring.indexOf("Welcome to the PIX firewall")>=0) && !logined) {
instring = "";
transmit("en fhq#0801\r");
logined =true;
} //whenever "Password" prompted
if(instring.indexOf("PIX passwd:")>=0) {
instring = "";
transmit("fhq#0731\r");
}
//whenever "Welcome" prompted
if((instring.indexOf("pixfirewall#")>=0) && !lsed) {
instring = "";
transmit("show uauth \r");
lsed = true;
}
//whenever "user 'xxxx' at ..." output
if(instring.indexOf("authorized to:")>=0) {
username = instring.split("'")[1];
ipaddress = instring.split("at")[1].split(",")[0];
ipaddress = ipaddress.trim(); instring = "";
}
}
catch(IOException e)
{ shutdown();}
if (c == -1) shutdown();
if (debug) System.out.println("Got char "+String.valueOf(c)+" = "+String.valueOf((char)c));
return (char)c;
}
// shutdown
// Terminate connection
void shutdown()
{
display("\nConnection closed\n");
s = null; in = null; out = null;
Thread.currentThread().interrupt();
} // display
// Display a string in the telnet window
void display(String str)
{
int i;
for(i=0; i<str.length(); i++)
displaych(str.charAt(i));
} // displaych
// Display one character at the current cursor position
void displaych(char c)
{
System.out.println("displaych():");
if (c == '\n') {
// Newline
System.out.println(); }
else if (c == '\t') {
// Tab
int i;
for(i=8; i>x%8; i--)
displaych(' ');
}
else if (c == (char)8) {
// Backspace
System.out.print("backspace");
}
else if (c >= 32 && c < 127) {
// Some printable character
System.out.print(c);
}
else if (c == 255) {
// Telnet IAC
char cmd = readch();
char opt = readch(); switch(opt) {
case 1: // echo
if (cmd == 251) echo = false;
else if (cmd == 252) echo = true;
break; case 3: // supress go-ahead
break; case 24: // terminal type
if (cmd == 253) {
// IAC WILL terminal-type
transmitch((char)255);
transmitch((char)251);
transmitch((char)24);
// IAC SB terminal-type IS <term> IAC SE
transmitch((char)255);
transmitch((char)250);
transmitch((char)24);
transmitch((char)0);
transmit(term);
transmitch((char)255);
transmitch((char)240);
}
else if (cmd == 250) {
while(readch() != 240)
;
}
break; default: // some other command
if (cmd == 253) {
// IAC DONT whatever
transmitch((char)255);
transmitch((char)252);
transmitch((char)opt);
}
break;
}
}
} void transmit(String str)
{
int i; for(i=0; i<str.length(); i++)
transmitch(str.charAt(i));
} void transmitch(char c)
{
if (c == '\n') transmitch('\r');
outstring = outstring + c;
try {
out.write((int)c);
out.flush();
}
catch(IOException e){ }; if (debug) System.out.println("Sent char " + String.valueOf((int)c) + " = " + String.valueOf(c));
}
}
你的server端运行了吗?
支持 tobejava(胆战心惊) !
真是太慷慨了!