如何用C语言编写一个简单的聊天室程序

如何用C语言编写一个简单的聊天室程序,第1张

这样:

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <netdb.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <sys/types.h>

#include <arpa/inet.h>

#include <pthread.h>

#define MAXLINE 100

void *threadsend(void *vargp)

void *threadrecv(void *vargp)

int main()

{

int *clientfdp

clientfdp = (int *)malloc(sizeof(int))

*clientfdp = socket(AF_INET,SOCK_STREAM,0)

struct sockaddr_in serveraddr

struct hostent *hp

bzero((char *)&serveraddr,sizeof(serveraddr))

serveraddr.sin_family = AF_INET

serveraddr.sin_port = htons(15636)

serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1")

if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) <0){

      printf("connect error\n")

      exit(1)

}

pthread_t tid1,tid2

printf("connected\n")

while(1){

pthread_create(&tid1,NULL,threadsend,clientfdp)

pthread_create(&tid2,NULL,threadrecv,clientfdp)

}

return EXIT_SUCCESS

}

void *threadsend(void * vargp)

{

//pthread_t tid2

int connfd = *((int *)vargp)

int idata

char temp[100]

while(1){

//printf("me: \n ")

fgets(temp,100,stdin)

send(connfd,temp,100,0)

printf("   友链       client send OK\n")

}

printf("client send\n")

return NULL

}

void *threadrecv(void *vargp)

{

char temp[100]

int connfd = *((int *)vargp)

while(1){

int idata = 0

idata = recv(connfd,temp,100,0)

if(idata >0){

printf("server :\n%s\n",temp)

}

}

return NULL

}

扩展资料:

注意事项

linux下编译多线程代码时,shell提示找键告蠢不到 pthread_create函数,原因是 pthread.h不是linux系统默认加载的库文件,应该使用类似如下gcc命令进行编译稿陪:

gcc echoserver.c -lpthread -o echoserver

只要注意 -lpthread参数就可以了。

import java.awt.*

import java.awt.event.*

import javax.swing.*

import java.net.*

import java.io.*

public class ClientDemo01 {

public static void main(String[] args){

JFrame f=new JFrame("AA")

JPanel p1=new JPanel()

JPanel p2=new JPanel()

JTextArea ta=new JTextArea(15,30)

ta.setEditable(false) /游友/文本域只神陵槐读

JScrollPane sp=new JScrollPane(ta) //滚动窗格

JTextField tf=new JTextField(20)

JButton b=new JButton("发送")

p1.add(sp)

p2.add(tf)

p2.add(b)

f.add(p1,"Center")

f.add(p2,"South")

f.setBounds(300,300,360,300)

f.setVisible(true)

f.setResizable(false)

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Socket socket=null

BufferedInputStream bis=null

BufferedOutputStream bos=null

try{

socket=new Socket("192.168.0.4",5000)

bis=new BufferedInputStream(socket.getInputStream())

bos=new BufferedOutputStream(socket.getOutputStream())

MyThread01 mt=new MyThread01(bis,ta)

mt.start()

}catch(Exception e){

e.printStackTrace()

}

b.addActionListener(new ButtonActionListener01(tf,ta,bos))

}

}

class ButtonActionListener01 implements ActionListener{

JTextField tf

JTextArea ta

BufferedOutputStream bos

public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf

this.ta=ta

this.bos=bos

}

public void actionPerformed(ActionEvent e){

String message=tf.getText()

if(!message.equals("")){

tf.setText("") //清空文本框

ta.append("AA:"+message+"\n") //添加到文本域并换行汪袭

try{

bos.write(message.getBytes())

bos.flush()

}catch(Exception ex){

System.out.println("发送失败")

}

}

}

}

class MyThread01 extends Thread{

BufferedInputStream bis

JTextArea ta

public MyThread01(BufferedInputStream bis,JTextArea ta){

this.bis=bis

this.ta=ta

}

public void run(){

try{

while(true){

byte[] b=new byte[100]

int length=bis.read(b)

String message=new String(b,0,length)

ta.append("BB:"+message+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

} import java.awt.*

import java.awt.event.*

import javax.swing.*

import java.net.*

import java.io.*

public class ServerDemo01{

public static void main(String[] args){

JFrame f=new JFrame("BB")

JPanel p1=new JPanel()

JPanel p2=new JPanel()

JTextArea ta=new JTextArea(12,30) //文本域,第一个参数为行数,第二个参数为列数

ta.setEditable(false) //文本域只读

JScrollPane sp=new JScrollPane(ta) //滚动窗格

JTextField tf=new JTextField(20)

JButton b=new JButton("发送")

p1.add(sp)

p2.add(tf)

p2.add(b)

f.add(p1,"Center")

f.add(p2,"South")

f.setBounds(300,300,360,300)

f.setVisible(true)

f.setResizable(false)

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

ServerSocket server=null

Socket socket=null

BufferedInputStream bis=null

BufferedOutputStream bos=null

try{

server=new ServerSocket(5000)

//ta.append("等待AA连接...\n")

socket=server.accept()

//ta.append("AA已连接\n")

bis=new BufferedInputStream(socket.getInputStream())

bos=new BufferedOutputStream(socket.getOutputStream())

MyThread1 mt=new MyThread1(bis,ta)

mt.start()

}catch(Exception e){

e.printStackTrace()

}

b.addActionListener(new ButtonActionListener1(tf,ta,bos))

}

}

class ButtonActionListener1 implements ActionListener{

JTextField tf

JTextArea ta

BufferedOutputStream bos

public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf

this.ta=ta

this.bos=bos

}

public void actionPerformed(ActionEvent e){

String message=tf.getText() //获取文本框中的内容

if(!message.equals("")){

tf.setText("") //清空文本框

ta.append("BB:"+message+"\n") //添加到文本域并换行

try{

bos.write(message.getBytes())

bos.flush()

}catch(Exception ex){

System.out.println("发送失败!")

}

}

}

}

class MyThread1 extends Thread{

BufferedInputStream bis

JTextArea ta

public MyThread1(BufferedInputStream bis,JTextArea ta){

this.bis=bis

this.ta=ta

}

public void run(){

try{

while(true){

byte[] b=new byte[100]

int length=bis.read(b)

String message=new String(b,0,length)

ta.append("AA:"+message+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

}


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

原文地址:https://54852.com/yw/12436914.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存