我要一份用java网络编程写的点对点的两人聊天程序(TCP和UDP)?

我要一份用java网络编程写的点对点的两人聊天程序(TCP和UDP)?,第1张

Server端:

import java.io.*

import java.net.*

import java.applet.Applet

public class TalkServer{

public static void main(String args[]) {

try{

ServerSocket server=null

try{

server=new ServerSocket(4700)

}catch(Exception e) {

System.out.println("can not listen to:"+e)

}

Socket socket=null

try{

socket=server.accept()

}catch(Exception e) {

System.out.println("Error."+e)

}

String line

BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()))

PrintWriter os=new PrintWriter(socket.getOutputStream())

BufferedReader sin=new BufferedReader(new InputStreamReader(System.in))

System.out.println("Client:"+is.readLine())

line=sin.readLine()

while(!line.equals("bye")){

os.println(line)

os.flush()

System.out.println("Server:"+line)

System.out.println("Client:"+is.readLine())

line=sin.readLine()

}

os.close()

is.close()

socket.close()

server.close()

}catch(Exception e){

System.out.println("Error:"+e)

}

}

}

Client端:

import java.io.*

import java.net.*

public class TalkClient {

public static void main(String args[]) {

try{

Socket socket=new Socket("127.0.0.1",4700)

BufferedReader sin=new BufferedReader(new InputStreamReader(System.in))

PrintWriter os=new PrintWriter(socket.getOutputStream())

BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()))

String readline

readline=sin.readLine()//从系统标准输入读入一字符串

while(!readline.equals("bye")){

os.println(readline)

os.flush()

System.out.println("Client:"+readline)

System.out.println("Server:"+is.readLine())

readline=sin.readLine()//从系统标准输入读入一字符串

}

os.close()//关闭Socket输出流

is.close()//关闭Socket输入流

socket.close()//关闭Socket

}catch(Exception e) {

System.out.println("Error"+e)//出错,则打印出错信息

}

}

}

给你一个简单的实现吧,注意一定要先运行MyServer.java

//MyCilent.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyClient extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

PrintWriter out

public MyClient(){

tf=new JTextField(20)

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

JPanel pan=new JPanel()

JScrollPane jsp=new JScrollPane(tx)

add(jsp,"Center")

bt=new JButton("SEND")

bt.addActionListener(this)

pan.add(tf)

pan.add(bt)

add(pan,"South")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE CLIENT")

setSize(400,300)

setVisible(true)

try{

Socket socket=new Socket("127.0.0.1",1680)

out=new PrintWriter(socket.getOutputStream(),true)

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

String s

while(true){

s=sin.readLine()

tx.append("#Server Said#: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

tx.append("@Client Said@: "+tf.getText()+"\n")

out.println(tf.getText())

tf.setText("")

}

}

public static void main(String[] args){

MyClient mct = new MyClient()

}

}

//MyServer.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyServer extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

JScrollPane jsp

JPanel pan

PrintWriter out

public MyServer(){

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

jsp=new JScrollPane(tx)

tf=new JTextField(20)

bt=new JButton("SEND")

bt.addActionListener(this)

pan=new JPanel()

pan.add(tf)

pan.add(bt)

add(pan,"South")

add(jsp,"Center")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE SERVER")

setSize(400,300)

setVisible(true)

try{

ServerSocket server = new ServerSocket(1680)

Socket socket = server.accept()

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

out=new PrintWriter(socket.getOutputStream(),true)

while(true){

String s=sin.readLine()

tx.append("@Client Said@: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

String st = tf.getText()

tx.append("#Server Said#: "+st+"\n")

out.println(st)

tf.setText("")

}

}

public static void main(String[] args){

MyServer msr = new MyServer()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8045392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存