
使用java写的file server,书上有示例代码,但是一直无法连接socket。请问是不是还需要一些服务器配置?
客户端测试执行到Socketsoc=newSocket(………………);的时候就会无法连接,请问如何解决这个问题。两个程序都可以编译,但是运行服务器后运行客户端就会有异常...
客户端测试执行到Socket soc = new Socket(………………);
的时候就会无法连接,请问如何解决这个问题。
两个程序都可以编译,但是运行服务器后运行客户端就会有异常,是不是本身书上的代码页有问题?可否告知如何将它运行,完成传输,最后能举例说明如何r和w。谢谢!
如果可以成功解决这个问题,肯定会有追加分数的,求指教!!!
使用的书本是《java面向对象程序设计(清华版)》
服务器代码:
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class FileServer
{
public static void main (String[] args) throws IOException
{
String filename, rw, dir = "/tmp/";
BufferedInputStream in = new BufferedInputStream(System.in);
String line = getLine(in);
StringTokenizer tk = new StringTokenizer(line, " \r\t=&");
int c = tk.countTokens();
if ( c != 4 ) return;
filename = tk.nextToken();
if ( filename.equals("filename") )
filename = tk.nextToken();
else return;
rw = tk.nextToken();
if ( rw.equals("mode") )
rw = tk.nextToken();
else return;
line = getLine(in);
if ( line.equals("") || line.equals("\r") )
if ( rw.equals("r") )
readBack(dir + filename);
else
writeTo(dir + filename, in);
}
static String getLine(InputStream in) throws IOException
{
int c;
StringBuilder buf = new StringBuilder();
while ( (c = in.read()) > -1 && c != '\n' )
buf.append((char) c);
return buf.toString();
}
static void readBack(String file) throws IOException
{
int c;
FileInputStream infile = new FileInputStream(file);
while ( (c = infile.read() ) > -1 )
System.out.write(c);
}
static void writeTo(String file, InputStream in) throws IOException
{
int c;
FileOutputStream ofile = new FileOutputStream(file);
while ( (c = in.read() ) > -1 )
ofile.write(c);
}
}
客户端测试代码由于提问字数限制,只能通过图片放上来,抱歉! 展开
的时候就会无法连接,请问如何解决这个问题。
两个程序都可以编译,但是运行服务器后运行客户端就会有异常,是不是本身书上的代码页有问题?可否告知如何将它运行,完成传输,最后能举例说明如何r和w。谢谢!
如果可以成功解决这个问题,肯定会有追加分数的,求指教!!!
使用的书本是《java面向对象程序设计(清华版)》
服务器代码:
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class FileServer
{
public static void main (String[] args) throws IOException
{
String filename, rw, dir = "/tmp/";
BufferedInputStream in = new BufferedInputStream(System.in);
String line = getLine(in);
StringTokenizer tk = new StringTokenizer(line, " \r\t=&");
int c = tk.countTokens();
if ( c != 4 ) return;
filename = tk.nextToken();
if ( filename.equals("filename") )
filename = tk.nextToken();
else return;
rw = tk.nextToken();
if ( rw.equals("mode") )
rw = tk.nextToken();
else return;
line = getLine(in);
if ( line.equals("") || line.equals("\r") )
if ( rw.equals("r") )
readBack(dir + filename);
else
writeTo(dir + filename, in);
}
static String getLine(InputStream in) throws IOException
{
int c;
StringBuilder buf = new StringBuilder();
while ( (c = in.read()) > -1 && c != '\n' )
buf.append((char) c);
return buf.toString();
}
static void readBack(String file) throws IOException
{
int c;
FileInputStream infile = new FileInputStream(file);
while ( (c = infile.read() ) > -1 )
System.out.write(c);
}
static void writeTo(String file, InputStream in) throws IOException
{
int c;
FileOutputStream ofile = new FileOutputStream(file);
while ( (c = in.read() ) > -1 )
ofile.write(c);
}
}
客户端测试代码由于提问字数限制,只能通过图片放上来,抱歉! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询