服务端:import java.net.*;
import java.io.*;
class SocketSendDemo
{
public static void main(String[] args) throws Exception
{
InetAddress ia = InetAddress.getLocalHost();
printf(ia);
printf(ia.getHostAddress());//10.0.59.21
SendSocket send = new SendSocket(11010);
} public static void printf(Object obj)
{
System.out.println(obj);
}
}
class SendSocket
{
private DatagramSocket send;
SendSocket(int socket)throws Exception
{
ServerSocket ss = new ServerSocket(11211);
Socket s = ss.accept();
String address = s.getLocalAddress().getHostAddress();
printf(address); BufferedReader file =
new BufferedReader(new InputStreamReader(s.getInputStream()));
//
String line = null;
while((line = file.readLine()) != null)
{
printf(line);
}
s.shutdownInput();
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("服务端:hello!!");
out.flush();
out.newLine();
s.close();
file.close();
out.close();/*把它关了,就会报Exception in thread "main" java.net.SocketException: Socket closed*/
}
public static void printf(Object obj)
{
System.out.println(obj);
}
}客户端:import java.net.*;
import java.io.*;
class SocketReceiveDemo
{
public static void main(String[] args)throws Exception
{
ReceiveSocket receive = new ReceiveSocket(11010);
}
public static void printf(Object obj)
{
System.out.println(obj);
}
}
class ReceiveSocket
{
private DatagramSocket receive;
ReceiveSocket(int socket)throws Exception
{
Socket s = new Socket("10.0.59.21",11211); BufferedReader file =
new BufferedReader(new FileReader("123.txt"));
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in =
new BufferedReader(new InputStreamReader (s.getInputStream()));
String line = null;
while((line = file.readLine()) != null)
{
out.write(line);
out.flush();
out.newLine();
}
s.shutdownOutput();
line = in.readLine();
printf(line);
s.close();
in.close();
file.close();
out.close();/*把它关了,就会报Exception in thread "main" java.net.SocketException: Socket closed*/
} public void printf(Object obj)
{
System.out.println(obj);
}
}求解
import java.io.*;
class SocketSendDemo
{
public static void main(String[] args) throws Exception
{
InetAddress ia = InetAddress.getLocalHost();
printf(ia);
printf(ia.getHostAddress());//10.0.59.21
SendSocket send = new SendSocket(11010);
} public static void printf(Object obj)
{
System.out.println(obj);
}
}
class SendSocket
{
private DatagramSocket send;
SendSocket(int socket)throws Exception
{
ServerSocket ss = new ServerSocket(11211);
Socket s = ss.accept();
String address = s.getLocalAddress().getHostAddress();
printf(address); BufferedReader file =
new BufferedReader(new InputStreamReader(s.getInputStream()));
//
String line = null;
while((line = file.readLine()) != null)
{
printf(line);
}
s.shutdownInput();
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("服务端:hello!!");
out.flush();
out.newLine();
s.close();
file.close();
out.close();/*把它关了,就会报Exception in thread "main" java.net.SocketException: Socket closed*/
}
public static void printf(Object obj)
{
System.out.println(obj);
}
}客户端:import java.net.*;
import java.io.*;
class SocketReceiveDemo
{
public static void main(String[] args)throws Exception
{
ReceiveSocket receive = new ReceiveSocket(11010);
}
public static void printf(Object obj)
{
System.out.println(obj);
}
}
class ReceiveSocket
{
private DatagramSocket receive;
ReceiveSocket(int socket)throws Exception
{
Socket s = new Socket("10.0.59.21",11211); BufferedReader file =
new BufferedReader(new FileReader("123.txt"));
BufferedWriter out =
new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
BufferedReader in =
new BufferedReader(new InputStreamReader (s.getInputStream()));
String line = null;
while((line = file.readLine()) != null)
{
out.write(line);
out.flush();
out.newLine();
}
s.shutdownOutput();
line = in.readLine();
printf(line);
s.close();
in.close();
file.close();
out.close();/*把它关了,就会报Exception in thread "main" java.net.SocketException: Socket closed*/
} public void printf(Object obj)
{
System.out.println(obj);
}
}求解
close
public void close()
throws IOException关闭此套接字。
所有当前阻塞于此套接字上的 I/O 操作中的线程都将抛出 SocketException。 套接字被关闭后,便不可在以后的网络连接中使用(即无法重新连接或重新绑定)。需要创建新的套接字。 关闭此套接字也将会关闭该套接字的 InputStream 和 OutputStream。 如果此套接字有一个与之关联的通道,则关闭该通道。
抛出:
IOException - 如果关闭此套接字时发生 I/O 错误。
另请参见:
isClosed()
out.close();
s.close();
你至少应该有一端带有循环退出标记
也就是说假设你的接收端判断某行内容为exit表示终止接收while((line = file.readLine()) != null) {
if(line.equals("exit")
break; out.write(line);
out.flush();
out.newLine();
}
break;