java简易通信源代码 通信 java
如何使用java实现两台计算机的通信 源代码
一个简单的聊天程序
创新互联公司2013年成立,先为谷城等服务建站,谷城等地企业,进行企业商务咨询服务。为谷城企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
参考一下;
//客户端的
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc=new ChatClient();
cc.receive();
}
JTextField jtf;
JTextArea jta;
Socket s;
PrintWriter out;
BufferedReader in;
public ChatClient(){
JFrame frame=new JFrame("ChatClient");
frame.setSize(400,300);
jta=new JTextArea();
jta.setEditable(false);
jtf=new JTextField();
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
s=new Socket("127.0.0.1",9000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
while(true){
try {
String text=in.readLine();
this.jta.append(text+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send(){
String text=this.jtf.getText();
this.jtf.setText("");
out.println(text);
out.flush();
}
}
//服务器端的
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9000);
List list=new ArrayList();
while(true){
Socket s=ss.accept();
list.add(s);
Thread t=new ServerThread(s,list);
t.start();
}
}
}
class ServerThread extends Thread{
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) {
this.s = s;
this.list = list;
try {
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) return;
Iterator it=list.iterator();
while(it.hasNext()){
Socket socket=(Socket)(it.next());
PrintWriter o=new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}
javaweb 做一个通讯录的小项目 求源代码 急求。
先设计数据库中通讯录表格(字段有:id,联系人姓名,手机号,备注),然后,hebinate实现数据库表到javabean的映射,同时也会生成对通讯录表格的增删改查的基本sql语句对应的接口。然后你再写一个Servlet,连接页面自己数据库操作接口即可
求编写一个超级简单的Java的程序源代码
class Stack_Float
{
float nums[];
int top;
Stack_Float()
{
nums = new float[50];
top = -1;
}
boolean IsEmpty()
{
if(top == -1)
return true;
else
return false;
}
float Pop_Stack()
{
if(top==-1)
{
return 0;
}
top--;
return nums[top + 1];
}
float GetTop()
{
return nums[top];
}
void Push_Stack(float num)
{
if(top == 49)
return;
top++;
nums[top] = num;
}
}
/*****************************************************************************/
class Stack_Char
{
char str[];
int top;
Stack_Char()
{
str = new char[50];
top = -1;
}
boolean IsEmpty()
{
if(top==-1)
return true;
else
return false;
}
void Push_Stack(char ch)
{
if(top == 49)
return;
top++;
str[top] = ch;
}
char Pop_Stack()
{
if(top == -1)
return '\0';
top--;
return str[top + 1];
}
char GetTop()
{
if(top == -1)
{
System.out.print("error");
System.exit(0);
}
return str[top];
}
}
/*****************************************************************************/
public class jisuanqi extends javax.swing.JFrame
{
String show = "";
public jisuanqi()
{
initComponents();
}
char[] TranSmit(char str[])
{
char houzhui[] = new char[50]; //存放后缀表达式的字符串
int i = 0,j = 0;
char c = str[i];
Stack_Char s = new Stack_Char(); //存放运算符的栈
while(c != '=') //对算术表达式扫描未结束时
{
if(c = '0' c = '9')
{
while(c = '0' c = '9')//数字直接入栈
{
houzhui[j]=c;
j++;
i++;
c=str[i];
}
houzhui[j]='#';//用#隔开数字
j++;
}
switch(c) //扫描到运算符时
{
case '+':
case '-':
case '*':
case '/':
if(s.IsEmpty() == true) //栈空,直接入栈
{
s.Push_Stack(c);
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == -1)
{
s.Push_Stack(c); //入栈
i++;
c=str[i];
break;
}
if(ComPare(s.GetTop(),c) == 1)
{
houzhui[j]=s.Pop_Stack();//出栈元素存入后缀表达式
j++;
break;
}
}
}
while(s.IsEmpty() != true)//把剩余的运算符直接出栈
{
houzhui[j]=s.Pop_Stack();
j++;
}
houzhui[j] = '=';//后缀表达式后面加 =
j++;
houzhui[j] = '\0';
j++;
return houzhui;
}
float Count(char str[])
{
Stack_Float s = new Stack_Float();//定义存放数字的栈
char c = str[0];
int i = 0,j = 0;
float result = 0,temp,left,right;
while(c != '=') //未扫描到 = 时
{
if(c = '0' c = '9')//扫描到数字
{
temp = 0;
while(c != '#')//未读到分隔符时
{
temp = temp * 10 + c - '0';
i++;
c = str[i];
}
s.Push_Stack(temp);//进栈
}
switch(c)//扫描到运算符时
{
case '+':
{
result = s.Pop_Stack() + s.Pop_Stack();//2个数字出栈相加
s.Push_Stack(result);//最后得数进栈
break;
}
case '-':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left - right;
s.Push_Stack(result);
break;
}
case '*':
{
result = s.Pop_Stack() * s.Pop_Stack();//2个数字出栈相乘
s.Push_Stack(result);
break;
}
case '/':
{
right = s.Pop_Stack();//右操作数出栈
left = s.Pop_Stack();//左操作数出栈
result = left / right;
s.Push_Stack(result);
break;
}
}
i++;
c = str[i];
}
return result;
}
int ComPare(char a,char b) //判断运算符的优先级函数
{
int s[][] =
{// 栈顶元素高于算术表达式中的元素时, 返回 1,否则返回 -1
{1,1,-1,-1},
{1,1,-1,-1},
{1,1,1,1},
{1,1,1,1},
};
char x1[]={'+','-','*','/'};//栈顶元素
char x2[]={'+','-','*','/'};//算术表达式中的元素
int k=0,m,n = 0;
for(m=0;m4;m++) //查找2个进行比较的运算符在表中的位置,并返回比较结果
{
for(n=0;n4;n++)
{
if(x1[m]==ax2[n]==b)
{
k=1;break; //找到比较结果后,跳出循环
}
}
if(k==1)
break;
}
return s[m][n];//返回比较结果
}
/*****************************************************************************/
@SuppressWarnings("unchecked")
// editor-fold defaultstate="collapsed" desc="Generated Code"//GEN-BEGIN:initComponents
private void initComponents() {
text = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton21 = new javax.swing.JButton();
jButton22 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().add(text);
text.setBounds(10, 10, 270, 30);
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(10, 50, 60, 25);
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(80, 50, 60, 25);
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(150, 50, 60, 25);
jButton4.setText("4");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
getContentPane().add(jButton4);
jButton4.setBounds(220, 50, 60, 25);
jButton5.setText("5");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
getContentPane().add(jButton5);
jButton5.setBounds(10, 80, 60, 25);
jButton6.setText("6");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
getContentPane().add(jButton6);
jButton6.setBounds(80, 80, 60, 25);
jButton7.setText("7");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
getContentPane().add(jButton7);
jButton7.setBounds(150, 80, 60, 25);
jButton8.setText("8");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
getContentPane().add(jButton8);
jButton8.setBounds(220, 80, 60, 25);
jButton9.setText("9");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
getContentPane().add(jButton9);
jButton9.setBounds(10, 110, 60, 25);
jButton10.setText("0");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
getContentPane().add(jButton10);
jButton10.setBounds(80, 110, 60, 25);
jButton11.setText("+");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
getContentPane().add(jButton11);
jButton11.setBounds(150, 110, 60, 25);
jButton12.setText("-");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
getContentPane().add(jButton12);
jButton12.setBounds(220, 110, 60, 25);
jButton13.setText("*");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
getContentPane().add(jButton13);
jButton13.setBounds(10, 140, 60, 25);
jButton14.setText("/");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
getContentPane().add(jButton14);
jButton14.setBounds(80, 140, 60, 25);
jButton21.setText("CE");
jButton21.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton21ActionPerformed(evt);
}
});
getContentPane().add(jButton21);
jButton21.setBounds(150, 140, 60, 25);
jButton22.setText("=");
jButton22.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton22ActionPerformed(evt);
}
});
getContentPane().add(jButton22);
jButton22.setBounds(220, 140, 60, 25);
pack();
}// /editor-fold//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
show += "1";
text.setText(show);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
show += "2";
text.setText(show);
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
show += "3";
text.setText(show);
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
show += "4";
text.setText(show);
}//GEN-LAST:event_jButton4ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
show += "5";
text.setText(show);
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
show += "6";
text.setText(show);
}//GEN-LAST:event_jButton6ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
show += "7";
text.setText(show);
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
show += "8";
text.setText(show);
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
show += "9";
text.setText(show);
}//GEN-LAST:event_jButton9ActionPerformed
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
show += "0";
text.setText(show);
}//GEN-LAST:event_jButton10ActionPerformed
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
show += "*";
text.setText(show);
}//GEN-LAST:event_jButton13ActionPerformed
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
show += "/";
text.setText(show);
}//GEN-LAST:event_jButton14ActionPerformed
private void jButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton21ActionPerformed
show = "";
text.setText("");
}//GEN-LAST:event_jButton21ActionPerformed
private void jButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton22ActionPerformed
show += "=";
text.setText(show);
char str1[] = new char[50];
char str2[] = new char[50];
float result = 0;
str1 = show.toCharArray();
str2 = TranSmit(str1);
result = Count(str2);
text.setText("" + result);
show = "";
}//GEN-LAST:event_jButton22ActionPerformed
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
show += "-";
text.setText(show);
}//GEN-LAST:event_jButton12ActionPerformed
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
show += "+";
text.setText(show);
}//GEN-LAST:event_jButton11ActionPerformed
public static void main(String args[])
{
jisuanqi j = new jisuanqi();
j.setBounds(300, 300, 300, 195);
j.setVisible(true);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton21;
private javax.swing.JButton jButton22;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JTextField text;
// End of variables declaration//GEN-END:variables
}
高分求两个简单的JAVA设计源代码
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+this.getTotalScore();
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个student类、StudentXW类、StudentBZ类
Student student1 = new Student("s001","张三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
socket JAVA 源代码
很久以前做的了,启动程序两次,在单选框中选服务器点连接(一定要先点服务器-连接),在在另外一个界面中选客户端点连接;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class QQ extends JFrame implements ActionListener{
public static void main(String args[]){
QQ qq=new QQ();
}
String input;
ServerSocket ss;
Socket s1,s2;
PrintWriter pw;
BufferedReader br;
private server s;
private client cc;
private JLabel l1,l2,l3,l4,l5;
private JRadioButton jb[]=new JRadioButton[2];
private JTextField jf1,jf2,jf3;
private JButton j1,j2,j3;
private JTextArea ja;
public QQ(){
super("聊天");
Container c=getContentPane();
c.setLayout(null);
l1=new JLabel("TCP通信程序");
l1.setFont(new Font("宋体",Font.BOLD,16));
l1.setBackground(Color.black);
l1.setSize(2000,20);
l1.setLocation(10,10);
c.add(l1);
String str1[]={"服务端","客户端"};
ButtonGroup bg=new ButtonGroup();
for(int x=0;xstr1.length;x++)
{
jb[x]=new JRadioButton(str1[x]);
jb[x].setFont(new Font("宋体",Font.BOLD,15));
jb[x].setForeground(Color.black);
jb[x].setSize(80,40);
jb[x].setLocation(10+x*80,37);
bg.add(jb[x]);
c.add(jb[x]);
}
jb[0].setSelected(true);
l2=new JLabel("连接主机IP");
l2.setFont(new Font("宋体",Font.BOLD,16));
l2.setBackground(Color.black);
l2.setSize(120,20);
l2.setLocation(20, 80);
c.add(l2);
jf1=new JTextField("127.0.0.1");
jf1.setSize(220,30);
jf1.setLocation(120, 80);
c.add(jf1);
jf3=new JTextField("离线");
jf3.setSize(150,30);
jf3.setLocation(280, 40);
c.add(jf3);
l5=new JLabel("连接状态:");
l5.setFont(new Font("宋体",Font.BOLD,16));
l5.setBackground(Color.black);
l5.setSize(120,20);
l5.setLocation(200, 47);
c.add(l5);
j1=new JButton("连接");
j1.setSize(110,20);
j1.setLocation(360,85);
j1.addActionListener(this);
c.add(j1);
l3=new JLabel("接收到的信息");
l3.setFont(new Font("宋体",Font.BOLD,16));
l3.setBackground(Color.black);
l3.setSize(120,20);
l3.setLocation(20, 130);
c.add(l3);
ja=new JTextArea();
ja.setSize(250,200);
ja.setLocation(130, 130);
c.add(ja);
l4=new JLabel("发送信息");
l4.setFont(new Font("宋体",Font.BOLD,16));
l4.setBackground(Color.black);
l4.setSize(120,20);
l4.setLocation(20, 340);
c.add(l4);
jf2=new JTextField("gf");
jf2.setSize(220,30);
jf2.setLocation(120, 340);
c.add(jf2);
j2=new JButton("发送信息");
j2.setSize(110,20);
j2.setLocation(360,350);
j2.addActionListener(this);
c.add(j2);
j3=new JButton("结束连接");
j3.setSize(110,20);
j3.setLocation(360,110);
j3.addActionListener(this);
c.add(j3);
s=new server();
cc=new client();
j3.setEnabled(false);
j2.setEnabled(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,450);
setVisible(true);
setLocation(300,300);
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==j1)
{
try{
if(jb[0].isSelected()==true)
{
input="";
s.start();
}
else {
input="";
cc.start();
}
}
catch(Exception ee)
{
jf3.setText("发生错误");
}
}
if(e.getSource()==j2)
{
pw.write(jf2.getText()+"\n");
pw.flush();
}
if(e.getSource()==j3)
{
try
{
if(jb[0].isSelected()==true)
{ s1.close();
jf3.setText("离线");
j2.setEnabled(false);
j3.setEnabled(false);
}
else
{
s2.close();
jf3.setText("离线");
j2.setEnabled(false);
j3.setEnabled(false);
}
}
catch (Exception e1) {
// TODO 自动生成 catch 块
}
}
}
class server extends Thread{
public void run(){
try {
j1.setEnabled(false);
jf3.setText("正在连接中@");
ss=new ServerSocket(4000);
s1=ss.accept();
br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
pw=new PrintWriter(s1.getOutputStream(),true);
// bs=new BufferedOutputStream(os);
while(true){
if(ss.isBound()==true){
jf3.setText("连接成功");
j2.setEnabled(true);
j3.setEnabled(true);
break;
}
}
while(true)
{
input=br.readLine();
if(input.length()0){
ja.append(input);
ja.append("\n");
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
}
}
}
class client extends Thread{
public void run(){
try {
j1.setEnabled(false);
jf3.setText("正在连接中@");
s2=new Socket(InetAddress.getByName(jf1.getText()),4000);
// s2=new Socket();
// s2.connect(new InetSocketAddress(jf1.getText(),21),1000);
br=new BufferedReader(new InputStreamReader(s2.getInputStream()));
pw=new PrintWriter(s2.getOutputStream(),true);
// bs=new BufferedOutputStream(os);
while(true){
if(s2.isConnected()==true){
jf3.setText("连接成功");
j2.setEnabled(true);
j3.setEnabled(true);
break;
}
}
input="";
while(true){
input=br.readLine();
if(input.length()0)
{
ja.append(input);
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
}
}
}
}
怎么:实现两台计算机之间交互式数据通信的源代码。以窗口实现交互的 java
一个简单的聊天程序
参考一下;
//客户端的
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc=new ChatClient();
cc.receive();
}
JTextField jtf;
JTextArea jta;
Socket s;
PrintWriter out;
BufferedReader in;
public ChatClient(){
JFrame frame=new JFrame("ChatClient");
frame.setSize(400,300);
jta=new JTextArea();
jta.setEditable(false);
jtf=new JTextField();
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
s=new Socket("127.0.0.1",9000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
while(true){
try {
String text=in.readLine();
this.jta.append(text+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send(){
String text=this.jtf.getText();
this.jtf.setText("");
out.println(text);
out.flush();
}
}
//服务器端的
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9000);
List list=new ArrayList();
while(true){
Socket s=ss.accept();
list.add(s);
Thread t=new ServerThread(s,list);
t.start();
}
}
}
class ServerThread extends Thread{
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) {
this.s = s;
this.list = list;
try {
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) return;
Iterator it=list.iterator();
while(it.hasNext()){
Socket socket=(Socket)(it.next());
PrintWriter o=new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}
本文标题:java简易通信源代码 通信 java
标题URL:http://myzitong.com/article/hpphog.html