import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.io.*
import java.net.*/*6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。
* 客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。
* (20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)*/
public class N4BT6 extends Frame
{
DatagramSocket socket
DatagramPacket packet byte[] buf
File file
FileInputStream input
String message = "该文件不存在"
TextArea text
public N4BT6(String title)
{
super(title)
text = new TextArea(6,4)
add(text)
setSize(400, 300)
setVisible(true)
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose()
}
})
buf = new byte[1024]
try
{
socket = new DatagramSocket(1230)
packet = new DatagramPacket(buf, buf.length)
socket.receive(packet)
file = new File(new String(packet.getData()))
socket = new DatagramSocket()
}
catch (Exception e)
{e.printStackTrace()
}
if(file.exists())
{
try
{
buf = new byte[(int)file.length()]
packet = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),1234)
input = new FileInputStream(file)
input.read(buf)
socket.send(packet)
}
catch (IOException e)
{
e.printStackTrace()
}
}
else
{
try
{
packet = new DatagramPacket(message.getBytes(),message.getBytes().length,
InetAddress.getLocalHost(),1234)
socket.send(packet)
}
catch (Exception e)
{
e.printStackTrace()
}
}
}
public static void main(String[] args)
{
new N4BT6("Server")
}
}
客户端:import java.awt.*
import java.awt.event.*
import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddresspublic class N4BT6_2 extends Frame
{
TextArea text
String message = "Q.txt"
DatagramSocket socket
DatagramPacket packet
byte[] buf
public N4BT6_2(String title)
{
super(title)
text = new TextArea(6,4)
add(text)
setSize(400, 300)
setVisible(true)
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose()
}
})
try
{
socket = new DatagramSocket()
packet = new DatagramPacket(message.getBytes(),message.getBytes().length,
InetAddress.getLocalHost(),1230)
socket.send(packet)
}
catch (Exception e)
{
e.printStackTrace()
}
try
{
buf = new byte[1024]
socket = new DatagramSocket(1234)
packet = new DatagramPacket(buf,buf.length)
socket.receive(packet)
text.append(new String(buf))
}
catch (Exception e)
{
e.printStackTrace()
}
}
public static void main(String[] args)
{
new N4BT6_2("Client")
}
}
服务器端:public class Server{
public static void main(String[] args){
ServerSocket ss = new ServerSocket(端口号)
Socket s = ss.accept()
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()))
String str = br.readLine()
System.out.println(str)
br.close()
s.close()
ss.close()
}
}
客户端:
public class Client{
public static void main(String[] args){
Socket s = new Socket(ip,端口)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()))
bw.write("hello")
bw.flush()
bw.close()
s.close()
}
}
先启动服务器端在启动客户端,两个端口要一致,如果是同一台电脑的ip可写为"127.0.0.1"
搂主说得很对,这个必须使用多线程得原理来做,一个线程用于监听后台事件,一个线程用于客户得操作即:一个线程初始化类用于服务器端的双线程产生
服务端所有操作都必须是以线程得形式出来(当然如果你能自己实现一个线程池是最好的)。
客户端则是平常得Socket操作
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)