try{
Socket mySocket = new Socket(“http://www.weixueyuan.net” ,1860);
}catch(IOException e){}
try{
ServerSocket serverSocket = new ServerSocket(1860);
}catch(IOException e){}
try{
Socket sc = serverSocket.accept();//ac是一个Socket对象
}catch(IOException e){}
sc.close();
import java.io.*;
import java.net.*;
public class Client{
public static void main(String args[]){
String s = null;Socket mySocket;
DataInputStream in = null;DataOutputStream out = null;
try{
mySocket = new Socket(“localhost”,4441);
in = new DataInputStream(mySocket.getInputStream());
out = new DataOutputStream(mySocket.getOutputStream());
out.writeUTF(“good server!”);
while(true){
s = in.readUTF();
if(s==null) break;
else System.out.println(s);
}
mySocket.close();
}catch(IOException e){
System.out.println(“can't connect”);
}
}
}
import java.io.*;import java.net.*;
public class Server{
public static void main(String args[]){
ServerSocket server = null;
Socket you = null;String s = null;
DataOutputStream out = null;
DataInputStream in = null;
try{
server = new ServerSocket(4441);
}catch(IOException e1){
system.out.println(“ERROR:” +e1);
}
try{
you = server.accept();
in = new DataInputStream(you.getInputStream());
out = new DataOutputStream(you. getOutputStream());
while(true){
s = in.readUTF();
if(s!=null) break;
}
out.writeUTF(“客户,你好,我是服务器”);
out.close();
}
catch(IOException e){System.out.println(“ERROR:”+e);}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class Aclient extends Applet implements Runnable,ActionListener{
JButton button; JTextField textF; JTextArea textA;
Socket socket; Thread thread;
DataInputStream in; DataOutputStream out;
public void init(){
setBackground(new Color(120,153,137));
setLayout(new BorderLayout());
Button = new JButton(“发送信息”);
textF = new JTextField(20);
textA = new JTextArea(20,30);
setSize(450,350);
JPanel p = new JPanel();
p.add(textF); p.add(button);
add(textA,”Center”); add(p,”South”);
button.addActionListener(this);
}
public void start(){
try{
socket = new Socket(this.getCodeBase().getHost(),4441);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}catch(IOException e){}
if(thread==null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run(){
String s = null;
while(true){
try{
s = in.readUTF();
}catch(IOException e){}
if(s.equals(“结束”)){
try{
socket.close();break;
}catch(IOException e){}
}else texA.append(s + “\n”);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
String s = textF.getText();
if(s! = null){
try{
out.writeUTF(s);
}catch(IOException e1){}
}
else{
try{
out.writeUTF(“请说话”);
}
catch(IOException e1){}
}
}
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class Aclient extends Applet implements Runnable,ActionListener{
JButton button; JTextField textF; JTextArea textA;
Socket socket; Thread thread;
DataInputStream in; DataOutputStream out;
public void init(){
setBackground(new Color(120,153,137));
setLayout(new BorderLayout());
Button = new JButton(“发送信息”);
textF = new JTextField(20);
textA = new JTextArea(20,30);
setSize(450,350);
JPanel p = new JPanel();
p.add(textF); p.add(button);
add(textA,”Center”); add(p,”South”);
button.addActionListener(this);
}
public void start(){
try{
socket = new Socket(this.getCodeBase().getHost(),4441);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}catch(IOException e){}
if(thread==null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void run(){
String s = null;
while(true){
try{
s = in.readUTF();
}catch(IOException e){}
if(s.equals(“结束”)){
try{
socket.close();break;
}catch(IOException e){}
}else texA.append(s + “\n”);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
String s = textF.getText();
if(s! = null){
try{
out.writeUTF(s);
}catch(IOException e1){}
}
else{
try{
out.writeUTF(“请说话”);
}catch(IOException e1){}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有