我怎样才能做一个程序,可以做消息.....如果我发送消息,它是由用户和用户回复达到我..套接字编程。 c#.net
0
A
回答
0
检查此代码,它是从应用程序与java编码的第二个通信:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
public class TcpCommunication
{
private TcpListener commListener;
private TcpClient client;
private StreamReader reader;
public TcpCommunication(int port)
{
this.commListener = new TcpListener(new IPAddress(new byte[]{127,0,0,1}),port);
}
public bool isAlive()
{
return client != null && this.client.Connected;
}
public void waitForClient()
{
this.commListener.Start();
this.client = commListener.AcceptTcpClient();
this.reader = new StreamReader(client.GetStream());
this.commListener.Stop();
}
public String getStringLine()
{
return reader.ReadLine();
}
public void writeStringLine(String commString)
{
commString = commString.Replace('\n','\t');
NetworkStream networkStream = client.GetStream();
System.Text.UTF8Encoding encoding = new UTF8Encoding();
Byte[] stringInByteFormat = encoding.GetBytes(commString + "\n"); networkStream.Write(stringInByteFormat,0,stringInByteFormat.Length);
}
0
有很多方法可以这样做。
- 您可以使用套接字(
System.Net.Sockets
命名空间,开始关注类TcpClient
和TcpListener
)。这是一个非常低层次的方法,你必须自己做所有事情。 - 您可以使用Web服务或WCF等通信编程现有的抽象层之一,我会推荐它。请参阅this question and answers开始阅读的地方。
0
你会发现很多很好的出发点,如果你约聊天程序的问题,以便搜索,因为它们使用套接字的对等网络通信的一个很好的例子,它们是互动的,所以你可以与他们进行实验直。例如:
https://stackoverflow.com/questions/1582097/java-chat-application
0
做到这一点,最好的方法是使用WCF(Windows通信基础),只是使用net.tcp绑定。
http://msdn.microsoft.com/library/dd943056.aspx
基本上,而不必创建和整个插座元帅的消息,你告诉在配置文件中创建一个WCF服务(在代码仅仅是接口和它的实现有几个属性),然后它监听一个tcp套接字而不是http。
您还可以将其配置为双工模式 - 这是双向模式。
相关问题
- 1. 套接字编程,C-java
- 2. C#套接字编程
- 3. C#套接字编程
- 4. 在c编程的套接字编程
- 5. 套接字编程 - C - 选择连接
- 6. recv()错误C/C++套接字编程
- 7. 使用C++或VC++进行套接字编程net
- 8. 套接字超时套接字编程
- 9. 套接字编程
- 10. 套接字编程
- 11. 套接字编程
- 12. 套接字编程
- 13. C中的Windows套接字编程
- 14. 亚马逊EC2套接字编程(C)
- 15. C++中的套接字编程
- 16. 新来的C#套接字编程
- 17. 套接字编程c - 文件下载
- 18. C++网络/套接字编程
- 19. Azure C#服务器套接字编程
- 20. C编程套接字缓冲区
- 21. C++中的UDP套接字编程
- 22. TCP套接字编程
- 23. CPP套接字编程
- 24. Android套接字编程
- 25. 套接字编程:recv()
- 26. android ndk编程:套接字
- 27. 什么是一个很好的教程/如何在.net/c#套接字编程
- 28. C套接字编程:重新打开并重新连接套接字
- 29. 套接字编程Java
- 30. 选择套接字编程
现在这个问题是*非常*一般。更具体的东西将允许更具体的答案。 – Richard 2010-12-16 09:22:15