用java编写一个能进行简单TCPIP通信的CS程序

用java编写一个能进行简单TCPIP通信的CS程序,第1张

import java.net.ServerSocket

import java.net.Socket

public class TcpServer

{

public static void main(String[] args) throws Exception

{

// 创建服务器端的socket对象

ServerSocket ss = new ServerSocket(5000)

// 监听连接

Socket socket = ss.accept()

// 直到连接建立好之后代码才会往下执行

System.out.println("Connected Successfully!")

}

}

import java.net.Socket

public class TcpClient

{

public static void main(String[] args) throws Exception

{

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

}

}

c/s程序,b/s程序都可以。可以编写基于服务器的b/s架构程序,如网站等,也可以c/s架构的,如qq等,当然也可以用Swing编写桌面应用软件,但这方面是java的弱项,所以建议用.net编写

package com.java.MyNotepad

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Font

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.ItemEvent

import java.awt.event.ItemListener

import java.awt.event.KeyEvent

import java.awt.event.KeyListener

import java.io.BufferedReader

import java.io.BufferedWriter

import java.io.File

import java.io.FileInputStream

import java.io.FileWriter

import java.io.IOException

import java.io.InputStreamReader

import javax.swing.JComboBox

import javax.swing.JFileChooser

import javax.swing.JFrame

import javax.swing.JMenu

import javax.swing.JMenuBar

import javax.swing.JMenuItem

import javax.swing.JOptionPane

import javax.swing.JScrollPane

import javax.swing.JTextArea

import javax.swing.filechooser.FileNameExtensionFilter

import com.java.find.FindString

public class MyNotepad {

private JFrame f

public static JTextArea ta

private JMenuBar mb

private JMenu fileMenu, edit, FColor, help

private JMenuItem mOpenFile, mSave, mExit, mFind, fRed, fGreen, fBlack,

fGray, fBlue, mAbout

private String[] fontTypeStr = { "宋体", "华文行楷", "隶书", "华文彩云", "华文新魏",

"华文琥珀", "幼圆", "楷书" }

private String fontStyleStr[] = { "常规", "倾斜", "粗体", "倾斜+粗体" }

private String fontSizeStr[] = { "13", "15", "20", "30", "40", "50", "60",

"70", "80", "100", "120", "150", "200", "300", "400", "500", "700",

"1000", "1500", "2000", "4000" }

private JComboBox fontt = new JComboBox(fontTypeStr)

private JComboBox fontStyle = new JComboBox(fontStyleStr)

private JComboBox fontSize = new JComboBox(fontSizeStr)

// 文件对话框

private JFileChooser Chooser

private File file

private int flag

private BufferedReader br

private BufferedWriter bw

private FileNameExtensionFilter filter

private int fileNum = 1

private FindString fs

public void initialized() {

f = new JFrame("MyNotepad")

// 多行文本框

ta = new JTextArea()

ta.addKeyListener(new Process())

// 滚动条

JScrollPane jsp = new JScrollPane(ta)

// 元素

fontt.addItemListener(new Process())

fontStyle.addItemListener(new Process())

fontSize.addItemListener(new Process())

mOpenFile = new JMenuItem("Open")

mOpenFile.addActionListener(new Process())

mSave = new JMenuItem("Save to...")

mSave.addActionListener(new Process())

mExit = new JMenuItem("Exit")

mExit.addActionListener(new Process())

mFind = new JMenuItem("Find")

mFind.addActionListener(new Process())

fRed = new JMenuItem("Red")

fRed.addActionListener(new Process())

fGreen = new JMenuItem("Green")

fGreen.addActionListener(new Process())

fBlack = new JMenuItem("Black")

fBlack.addActionListener(new Process())

fGray = new JMenuItem("Gray")

fGray.addActionListener(new Process())

fBlue = new JMenuItem("Blue")

fBlue.addActionListener(new Process())

mAbout = new JMenuItem("About")

mAbout.addActionListener(new Process())

// 菜单列表元素

fileMenu = new JMenu("File")

edit = new JMenu("Edit")

FColor = new JMenu("Font Color")

help = new JMenu("Help")

// 菜单

mb = new JMenuBar()

// 添加组件

fileMenu.add(mOpenFile)

fileMenu.add(mSave)

fileMenu.add(mExit)

fileMenu.setForeground(Color.WHITE)

edit.add(mFind)

edit.setForeground(Color.WHITE)

help.add(mAbout)

help.setForeground(Color.WHITE)

FColor.add(fRed)

FColor.add(fGreen)

FColor.add(fBlack)

FColor.add(fGray)

FColor.add(fBlue)

FColor.setForeground(Color.WHITE)

mb.add(fileMenu)

mb.add(edit)

mb.add(FColor)

mb.add(help)

mb.add(fontt)

mb.add(fontStyle)

mb.add(fontSize)

mb.setBackground(Color.gray)

ta.setBackground(Color.LIGHT_GRAY)

ta.setForeground(Color.black)

f.add(mb, BorderLayout.NORTH)

f.add(jsp, BorderLayout.CENTER)

f.setLocation(400, 200)

f.setSize(800, 450)

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

f.setVisible(true)

}

}

这是写记事本中的界面代码,功能代码没有贴上来,要不你看着感觉复杂了。。。如果要源码可以留邮箱,运行这段代码直接写个main函数实例化一个该类对象,调用initialized方法


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存